[ Pobierz całość w formacie PDF ]
.The statements thenecho the end-of-line characters so the message is displayed correctly.The script stops on the exitstatement.14_167779 bk02ch04.qxp 12/17/07 8:09 PM Page 253253Copying ObjectsIf no exception is thrown, the catchblock has nothing to catch, and it isignored.The script proceeds to the statements after the catchblock.Inthis case, if the amount of gas doesn t exceed 50 gallons, the catchblock is ignored, and the script proceeds to the statements after thecatchblock.If you run the preceding script, the following is displayed by the browser:10 gallons of gas were added45 gallons of gas were addedGas is overflowingBook IIThe second addGasmethod call raised the amount of gas over 50 gallons, soChapter 4an exception was thrown.The catchblock displayed the overflow messageand stopped the script.Copying ObjectsPHP provides a method you can use to copy an object.The method is__clone, with two underscores.You can write your own __clonemethodin a class if you want to specify statements to run when the object is copied.If you don t write your own, PHP uses its default __clonemethod thatcopies all the properties as is.As shown by the two underscores beginningits name, the clone method is a different type of method, and thus is calleddifferently, as shown in the following example.You could write the following class:class Car{private $gas = 0;private $color = red ;function addGas($amount){$this->gas = $this->gas + $amount;echo $amount gallons added to gas tank ;}function __clone(){$this->gas = 5;}}Using this class, you can create an object and copy it, as follows:$firstCar = new Car;$firstCar->addGas(10);$secondCar = clone $firstCar;Object-OrientedProgramming14_167779 bk02ch04.qxp 12/17/07 8:09 PM Page 254254 Comparing ObjectsAfter these statements, you have two cars:&' $firstCar: This car is red and contains 10 gallons of gas.The 10 gal-lons were added with the addGasmethod.&' $secondCar: This car is red, but contains 5 gallons of gas.The dupli-cate car is created using the _ _clonemethod in the Carclass.Thismethod sets gas to 5 and doesn t set $colorat all.If you didn t have a __clonemethod in the Carclass, PHP would use adefault __clonemethod that would copy all the properties, making$secondCarboth red and containing 10 gallons of gas.Comparing ObjectsAt their simplest, objects are data types.You can compare objects with theequal operator, which is two equal signs (==), or with the identical operator,which is three equal signs (===).Using the equal operator, two objects areequal if they are created from the same class and have the same propertiesand values.However, using the identical operator, two objects are identicalonly if they refer to the same instance of the same class.The following two objects are equal, but not identical, because they are twoinstances of the class Car:$my_car = new Car();$my_car2 = new Car();Thus, the following statement would echo equal:If($my_car == $my_car2){echo equal ;}But, the following statement would not echo equal:If($my_car === $my_car2){echo equal ;}The following two objects are equal, but not identical, because clonecre-ates a new instance of the object Car:$my_car = new Car();$my_car2 = clone $my_car;14_167779 bk02ch04.qxp 12/17/07 8:09 PM Page 255255Getting Informatin about Objects and ClassesThe following two objects are both equal and identical:$my_car = new Car();$my_car2 = $my_car;Getting Information about Objects and ClassesPHP provides several functions that you can use to get information aboutobjects and classes:&' You can check whether a class exists with the following:Book IIChapter 4class_exists( classname );&' You can test whether a property exists in a specific class with thefollowing:property_exists( classname , propertyname );&' You can find out the properties, with their defaults, and the methodsdefined in a class with the following statements:get_class_vars( classname );get_class_methods( classname );The get_class_functions return an array.The properties array con-tains the property name as the key and the default as the value.Themethods array contains numeric keys and the names of the methods asvalues.If a property or method is private, the function will not return itsname unless it is executed from inside the class [ Pobierz całość w formacie PDF ]