Page 224 - Beginning PHP 5.3
P. 224

Part II: Learning the Language

                    echo “ < h2 > Some properties: < /h2 > ”;
                    echo “ < p > My car’s manufacturer is “ . $myCar- > manufacturer . “. < /p > ”;
                    echo “ < p > My car’s engine size is “ . $myCar- > engineSize . “. < /p > ”;
                    echo “ < p > My car’s fuel type is “ . $myCar- > fuelType . “. < /p > ”;
                    echo “ < h2 > The \$myCar Object: < /h2 > < pre > ”;

                    print_r( $myCar );
                    echo “ < /pre > ”;

                    ? >
                       < /body >

                    </html>





































                       Figure 8 - 3


                  H ow  I t  W orks
                  This script creates the familiar  Car  class containing some fixed public properties  —  $manufacturer ,


                  $model , and  $color  —  and also adds a private array property,  $_extraData . The  Car  class
                also contains a   __get()  method that looks up the requested property name in the keys of the
                  $_extraData  array, returning the corresponding value in the array (if found). The corresponding  __set()
                method takes the supplied property name and value, and stores the value in the   $_extraData  array,
                keyed by the property name.


              186





                                                                                                      9/21/09   9:03:38 AM
          c08.indd   186                                                                              9/21/09   9:03:38 AM
          c08.indd   186
   219   220   221   222   223   224   225   226   227   228   229