Page 210 - Beginning PHP 5.3
P. 210

Part II: Learning the Language































                        Figure 8 - 1


                  Static Properties

                   You encountered static function variables in the previous chapter. You can also create static class
                 properties in a similar way, by adding the   static  keyword just before the property name:

                    class MyClass {
                      public static $myProperty;

                    }
                   Static members of a class are independent of any particular object derived from that class. To access
                 a static property, you write the class name, followed by two colons (  :: ), followed by the property name
                 (preceded by a   $  symbol):
                    MyClass::$myProperty = 123;

                   Here ’ s an example using the  Car  class:

                    class Car {
                      public $color;
                      public $manufacturer;
                      static public $numberSold = 123;
                    }

                    Car::$numberSold++;

                    echo Car::$numberSold; // Displays “124”

              172





                                                                                                      9/21/09   9:03:33 AM
          c08.indd   172
          c08.indd   172                                                                              9/21/09   9:03:33 AM
   205   206   207   208   209   210   211   212   213   214   215