Datasheet
18
x
CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK
} else {
$this->drivesCar = TRUE;
}
}
function returnCar() {
$this->rentedCar->returnCar($this->rentedCar);
}
function getMakeAndModel() {
if (TRUE == $this->drivesCar ) {
return ‘I drive ‘.$this->rentedCar->getMakeAndModel().’ really fast!’;
} else {
return “I can’t rent this car.”;
}
}
}
?>
code snippet /singleton/Customer.class.php
We can test these classes with the following code. It creates two customers, who both want to rent
the car at the same time. But the second one will have to wait until the car is returned.
<?php
include_once(‘Customer.class.php’);
$Customer_1 = new Customer();
$Customer_2 = new Customer();
echo ‘Customer_1 wants to rent the car. <br />’;
$Customer_1->rentCar();
echo ‘Customer_1 says: ‘ . $Customer_1->getMakeAndModel() . ‘<br />’;
echo ‘<br />’;
echo ‘Customer_2 wants to rent the car. <br />’;
$Customer_2->rentCar();
echo ‘Customer_2 says: ‘ . $Customer_2->getMakeAndModel() . ‘<br />’;
echo ‘<br />’;
$Customer_1->returnCar();
echo ‘Customer_1 returned the car.<br />’;
echo ‘<br />’;
echo ‘Customer_2 wants to rent the car. Again.’ . ‘<br />’;
$Customer_2->rentCar();
echo ‘Customer_2 says: ‘ . $Customer_2->getMakeAndModel() . ‘<br />’;
echo ‘<br />’;
?>
code snippet /singleton/Test.php
The output of this code will look like this:
Customer_1 wants to rent the car.
Customer_1 says: I drive Dodge Magnum really fast!
Customer_2 wants to rent the car.
Customer_2 says: I can’t rent this car.
Customer_1 returned the car.
c01.indd 18c01.indd 18 1/24/2011 5:45:19 PM1/24/2011 5:45:19 PM