HomePHP Page 2 - Introducing Static Members and Methods in PHP 5
Static members and methods: an example - PHP
Static properties and methods can be quite useful in a range of situations, not merely for constructing a Singleton class. This article, the first in a two-part series, introduces the basic concepts of static properties and methods, using plenty of hands-on examples.
Indeed, a good way to start understanding how static class properties and methods work when used with PHP 5, is simply by setting up a comprehensive example, which also can be handy for grasping the logic behind static methods.
For this reason, below I coded a basic class, which shows in a friendly fashion how to use both static properties and methods in a PHP 5-controlled environment. Please, take a look at the signature of this class:
static public function
Provided that you already examined the above class’ source code, then let me stress some key points regarding its definition. First off, this “Counter” class exposes a static “$counter” property, which is incremented each time the “incrementCounter()” method is called up.
However, one of the things worth noting here is how this method precisely increments the value of the mentioned property by using the “self” PHP keyword. Since this property has been initially declared static within the class, it shouldn’t be invoked with the pseudo variable $this, as you’d do normally with regular properties.
Now, once the previous “$counter” property has been properly explained, I’d like you to pay attention to the method responsible for incrementing it. As you can see, the method in question has been declared static too, therefore you’d be able to call it both from inside and outside the object context, as illustrated below:
As shown above, the mentioned “incrementCounter()” method can be invoked by using the conventional object context, that is with the “->” notation, or from outside this context, by using the (::) scope resolution operator. But after studying the prior script, probably one question keeps spinning in your mind: what’s the advantage of using the last approach?
Well, the answer is that a static method isn’t bounded to a specific class instance, which means that it can be called directly without having to create an object inside the application you’re developing. Pretty handy, isn’t it? After all, there are some situations where working with many objects can contribute to messing up an application’s source code, so static methods might be quite useful in those cases.
All right, now you have a better idea of how to declare and use static properties and methods with PHP 5 by using the “static” keyword. However, you’ll agree with me that the sample class that you learned is rather simplistic for using in real situations. Therefore, in the following section you’ll learn how to use static methods and properties in a more useful case: applying the Singleton pattern.
To see how this design pattern will be used, please click on the link shown below and continue reading.