[ Team LiB ] Previous Section Next Section

Static Properties

Static properties were also introduced with PHP 5. They are similar in some senses to class constants in that a static property is available via the class rather than the object. Static properties can be changed at runtime, however, making them useful when greater flexibility is required.

We declare a static variable with the static keyword:

class Item {
  public static $SALES_TAX=9;

Notice that we can also determine the access level for a static property. We access the static class property via the class name:

print "the tax to be levied on all items is";
print Item::$SALES_TAX;
print "%";

We could change Item::$SALES_TAX at any time, and the value would change for all instances of Item and for all client code.

    [ Team LiB ] Previous Section Next Section