Home arrow PHP arrow Page 3 - Introducing Static Members and Methods in PHP 5

Implementing the Singleton design pattern - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Undoubtedly, one of the typical cases where static members and methods are used conjunctly in the same class is when you’re implementing the Singleton pattern. As you probably know, this pattern allows you to work with only one instance of a given class, without having to get into trouble using multiple instances across the same application. This is a highly desirable feature generally required by seasoned developers.

However, I don’t want you to focus your whole attention on the pattern itself, since it’s out of the scope of this article. Instead, concentrate on the signature of the class below, so you can learn how a Singleton class can use both static properties and methods. Please look at the following class:

  // example of static property and method(uses the 'Singleton' pattern) class Singleton{ // declare static property static private $instance=NULL; private function__construct(){} // declare static method static public function getInstance(){        if(self::$instance==NULL){             self::$instance = new
Singleton() } return self::$instance; } } // return a new instance of 'Singleton' class $singA=Singleton::getInstance(); var_dump($singA); // return the same instance of 'Singleton' class $singB=Singleton::getInstance(); var_dump($singB);

Basically, the above class represents a classic definition of the Singleton pattern, where both static properties and methods are used together. In this case, the static “$instance” property is utilized as a flag to instruct the corresponding “getInstance()” method to return always a single instance of the class (hence the name Singleton), which helps with developing more robust code.

Also, it should be noticed that the proper “getInstance()” method has been declared static too, which means that it’s callable from outside the object content, as clearly demonstrated by the previous example (notice the use of the respective :: scope resolution operator). Indeed, a Singleton class is a great example for teaching how static members and methods can be used together in PHP.

Okay, I believe that the Singleton class that you just saw gave you a better understanding of how to code static properties and methods inside the same class. However, there’s a long way ahead of us when it comes to using them in PHP 5. That’s why in the course of the following section, I’ll be developing a brand new practical example, which will show you yet another case where static methods and properties are used conjunctly.

To continue reading and learn more, click on the link below.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: