Page 209 - Beginning PHP 5.3
P. 209

Chapter 8: Objects
                          Here ’ s an example that shows how to define properties then set and read their values:

                               < !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
                               < html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
                                < head >
                                  < title > Defining and Using Object Properties < /title >
                                  < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                                < /head >
                                < body >
                                  < h1 > Defining and Using Object Properties < /h1 >

                               < ?php

                             class Car {
                               public $color;
                               public $manufacturer;
                             }

                             $beetle = new Car();
                             $beetle- > color = “red”;
                             $beetle- > manufacturer = “Volkswagen”;

                             $mustang = new Car();
                             $mustang- > color = “green”;
                             $mustang- > manufacturer = “Ford”;

                             echo “ < h2 > Some properties: < /h2 > ”;
                             echo “ < p > The Beetle’s color is “ . $beetle- > color . “. < /p > ”;
                             echo “ < p > The Mustang’s manufacturer is “ . $mustang- > manufacturer . “. < /p > ”;
                             echo “ < h2 > The \$beetle Object: < /h2  > < pre > ”;

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

                             echo “ < h2 > The \$mustang Object: < /h2 > < pre > ”;
                             print_r( $mustang );
                             echo “ < /pre > ”;

                             ? >

                                 < /body >
                               < /html >

                           You can see the output from this script in Figure  8 - 1 . The script defines a class,  Car , with two public
                         properties,   $color  and  $manufacturer . Then it creates a new  Car  object and assigns it to a variable
                         called   $beetle , and sets  $beetle  ’ s  $color  and  $manufacturer  properties to  “red”  and
                           “Volkswagen” , respectively. Next the script creates another  Car  object, assigns it to  $mustang , and sets
                          its   $color  property to  “green”  and its  $manufacturer  property to  “Ford”.

                            Now that the two objects have been created and their properties set, the script displays the values of a
                          couple of properties: the   $color  property of the  $beetle  object ( $beetle - > color ) and the

                           $manufacturer  property of the  $mustang  object ( $mustang - > manufacturer ). Finally, the script uses

                            print_r()  to display the two objects; notice how  print_r()  displays an object ’ s properties in much
                          the same way as it displays array keys and values.

                                                                                                         171





                                                                                                      9/21/09   9:03:32 AM
          c08.indd   171
          c08.indd   171                                                                              9/21/09   9:03:32 AM
   204   205   206   207   208   209   210   211   212   213   214