PHP
  Home arrow PHP arrow Page 3 - Introducing Static Members and Methods in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introducing Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-10-02


    Table of Contents:
  • Introducing Static Members and Methods in PHP 5
  • Static members and methods: an example
  • Implementing the Singleton design pattern
  • Building an array processing factory
  • Building an array processor factory continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introducing Static Members and Methods in PHP 5 - Implementing the Singleton design pattern
    ( Page 3 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT