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? 
Google.com  
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

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek