PHP
  Home arrow PHP arrow Page 2 - 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 - 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
     

       

    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 6 Hosted by Hostway
    Stay green...Green IT