PHP
  Home arrow PHP arrow Page 2 - 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 - Static members and methods: an example


    (Page 2 of 5 )

    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


       · 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