Home arrow PHP arrow 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.

  1. Introducing Static Members and Methods in PHP 5
  2. Static members and methods: an example
  3. Implementing the Singleton design pattern
  4. Building an array processing factory
  5. Building an array processor factory continued
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
October 02, 2006

print this article



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:

class Counter{    static private $counter=0; static public function incrementCounter(){ self::$counter++; return self::$counter; } }

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:

$counter=new Counter; // call static method from inside object context echo $counter->incrementCounter(); // call method from outside object context echo Counter::incrementCounter();

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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: