Keywords: Can we have a little privacy in here?
You have already seen the public keyword in relation to property declarations. This keyword denotes a property's visibility. In fact, the visibility of a property can be set to public, private, and protected. Properties that are public can be written to and read from outside the class. Properties that are private can only be seen within the object or class context. Properties that are protected can only be seen within the context of the current class or its children. (You will see this in action in the Inheritance section.) You can use private properties to really lock down our classes. If you declare your properties private and attempt to access them from outside the class' scope (as shown in Listing 7), the PHP engine will throw a fatal error.
Listing 7. Attempting to access your properties from outside the class' scope
|
This outputs the following:
|
As a rule of thumb, you should make most properties private, then provide methods for getting and setting them if necessary. In this way, you can control a class' interface, making some data read-only, cleaning up or filtering arguments before assigning them to properties, and providing a clear set of rules for interacting with objects.
You can modify the visibility of methods in the same way as
properties, adding public, private, or protected to the method
declaration. If a class needs to use some housekeeping methods that the
outside world need not know about, for example, you can declare them
private. In Listing 8, a get()
method provides the interface for users of the Dictionary
class to extract a translation. The class also needs to keep track of all queries and provides a private method, logQuery()
, for this purpose.
Listing 8. A get() method provides the interface for users of the Dictionary class
|
Declaring logQuery()
as private simplifies the public interface and protects the class from having logQuery()
called inappropriately. As with properties, any attempt to call a
private method from outside the containing class causes a fatal error.
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.
Listing 9. A single static $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.
Listing 10. Two static methods that access the $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.
Listing 11. Setting 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.
View Getting started with objects with PHP V5 Discussion
Page: 1 2 3 4 5 6 Next Page: Inheritance