PHP
  Home arrow PHP arrow Page 3 - Introducing Static Members and Methods...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 7
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this first article of the series, the basic concepts on using static properties...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT