Getting Started with Objects with PHP V5
By Matt Zandstra2005-07-01
Working in class context
The methods and properties you have seen so far all operate in object context. That is, you must access them using an object instance, via the
$this pseudo-variable or an object reference stored in a standard variable. In some cases, you may find that it's more useful to access properties and methods via a class rather than an object instance. Class members of this kind are known as static.To declare a static property, place the keyword static after the visibility modifier, directly in front of the property variable.
This example shows a single static property: $iodir, which holds the path to the default directory for saving and reading Dictionary data. Because this data is the same for all objects, it makes sense to make it available across all instances.
$iodir property |
You can access a static property using the scope resolution operator, which consists of a double colon (::). The scope resolution operator should sit between the class name and the static property you wish to access.
|
As you can see, there is no need to instantiate a Dictionary object to access this property.
The syntax for declaring and accessing static methods is similar. Once again, you should place the static keyword after the visibility modifier. Listing 10 shows two static methods that access the $iodir property, which is now declared private.
$iodir property |
Users can no longer access the $iodir property directory. By creating special methods for accessing a property, you can ensure that any provided value is sane. In this case, the method checks that the given string points to a writable directory before making the assignment.
Notice that both methods refer to the $iodir property using the keywordself and the scope resolution operator. You cannot use $this in a static method because $this is a reference to the current object instance, but a static method is called via the class and not an object. If the PHP engine sees $this in a static method, it will throw a fatal error together with an informative message.
To call a static method from outside of its class, use the class name together with the scope resolution operator and the name of the method.
|
There are two good reasons why you might want to use a static method. First of all, a utility operation may not require an object instance to do its job. By declaring it static, you save client code the overhead of creating an object. Second, a static method is globally available. This means that you can set a value that all object instances can access, and it makes static methods a great way of sharing key data across a system.
While static properties are often declared private to prevent meddling, there is one way of creating a read-only statically scoped property: You can declare a constant. Like its global cousin, a class constant is immutable once defined. It is useful for status flags and for other things that don't change during the life of a process, like pi, for example, or all the countries in Africa.
You declare a class constant with the const keyword. For example, since a real-world implementation of a Dictionary object would almost certainly have a database sitting behind it, you can also assume that there will be a maximum length for terms and translations. Listing 11 sets this as a class constant.
MAXLENGTH as a class constant |
Class constants are always public, so you can't use the visibility keywords. This is not a problem because any attempt to change the value will result in a parse error. Also notice that unlike regular properties, a class constant does not begin with the dollar sign.
Tutorial Pages:
» Why you need to know objects and classes, and how to use them
» What are classes and objects?
» A first class
» Properties
» Methods
» The constructor
» Keywords: Can we have a little privacy in here?
» Working in class context
» Inheritance
» Summary
» Resources
First published by IBM DeveloperWorks
