PHP
  Home arrow PHP arrow Page 5 - 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 / 8
    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 - Building an array processor factory continued
    ( Page 5 of 5 )

    As I said before, an array processor factory is more than enough to demonstrate how static members and methods can be used in a helpful fashion. But how will this completely new class look?

    To answer this question, here’s the definition for the mentioned class:

    // define 'ArrayProcessorFactory' class (implements the Singleton pattern) class ArrayProcessorFactory{ private $allowedArrayProcessors=array
    ('ArrayToUppercase','ArrayToLowercase','ArrayToReverse'); static private $instance=NULL; private function__construct(){} // get instance of array factory static public function getInstance(){    if(self::$instance==NULL) {         self::$instance=new ArrayProcessorFactory(); } return self::$instance; } // create array processors public function createArrayProcessor($arrayProcessor){ if(!in_array($arrayProcessor,$this->
    allowedArrayProcessors)){ throw new Exception
    ('Invalid name for array processor'); } return new $arrayProcessor; } }

    As you can see, the above “ArrayProcessorFactory” class is indeed a factory that creates array processors, in accordance with the input passed to its “createArrayProcessor()” method. In this case, since I want to work only with a single instance of this class, I applied the definition of the Singleton pattern that you learned before to achieve this.

    In addition, defining the factory class in this way implies using the static $instance property, along with the respective static “getInstance()” method that you saw in previous examples. Now, do you see how static members and methods play a relevant role in the context of the above factory class? I hope you do!

    Okay, once the corresponding array processor factory has been defined, it’s time to see how it can be put to work. With reference to this subject, below I coded a short script that uses all the classes I defined earlier:

    try{ // get single instance of 'ArrayProcessorFactory' class    $procFactory=ArrayProcessorFactory::getInstance(); // create 'ArrayToUppercase' object   $arrayUp=$procFactory->createArrayProcessor('ArrayToUppercase'); // create 'ArrayToLowercase' object     $arrayLow=$procFactory->createArrayProcessor('ArrayToLowercase'); // create 'ArrayToReverse' object     $arrayRev=$procFactory->createArrayProcessor('ArrayToReverse'); // add elements to first array     $arrayUp->addArrayElement('This is element 1');     $arrayUp->addArrayElement('This is element 2');   $arrayUp->addArrayElement('This is element 3');   $arrayUp->addArrayElement('This is element 4'); $arrayUp->addArrayElement('This is element 5'); // convert array to uppercase    $arrayUp->processArray(); // save array to file     $arrayUp->saveArray(); // print uppercased array     print_r($arrayUp->getArray()); // add elements to second array   $arrayLow->addArrayElement('This is element 1');   $arrayLow->addArrayElement('This is element 2');    $arrayLow->addArrayElement('This is element 3');    $arrayLow->addArrayElement('This is element 4');   $arrayLow->addArrayElement('This is element 5'); // convert array to lowercase     $arrayLow->processArray(); // save array to file    $arrayLow->saveArray(); // print uppercased array     print_r($arrayLow->getArray()); // add elements to third array     $arrayRev->addArrayElement('This is element 1'); $arrayRev->addArrayElement('This is element 2');   $arrayRev->addArrayElement('This is element 3');    $arrayRev->addArrayElement('This is element 4');   $arrayRev->addArrayElement('This is element 5'); // reverse array    $arrayRev->processArray(); // save array to file    $arrayRev->saveArray(); // print uppercased array    print_r($arrayRev->getArray()); } catch(Exception $e){ echo $e->getMessage(); exit(); }

    As shown by the above script, I first used a single instance of the factory class --please examine the implementation of the “getInstance()” method -- in order to create different array processor objects. Then, I populated the pertinent arrays with some trivial values, which were processed and saved appropriately via the corresponding “processArray()” and “save()” methods respectively, and finally the results were outputted to the browser as follows:

    Array ( [0] => THIS IS ELEMENT 1 [1] => THIS IS ELEMENT 2 [2] =>
    THIS IS ELEMENT 3 [3] => THIS IS ELEMENT 4 [4] => THIS IS ELEMENT 5 ) Array ( [0] => this is element 1 [1] => this is element 2 [2] =>
    this is element 3 [3] => this is element 4 [4] => this is element 5 ) Array
    ( [0] => This is element 5 [1] => This is element 4 [2] => This is element 3 [3] => This is element 2 [4] => This is element 1 )

    The output shown above clearly demonstrates how both static methods and properties can be used conjunctly to apply, in the correct sequence, the Singleton and Factory patterns. As usual, feel free to tweak all code samples listed here, so you can acquire a better grounding in using static members and methods with PHP.

    Bottom Line

    In this first part of the series, you learned the basics of defining static members and properties with PHP 5, and used them in concrete cases, such as the implementation of the Singleton and Factory patterns respectively.

    However this instructive journey has just started. That’s why in the next tutorial, I’ll show you how to expand the application of static methods in PHP 5, demonstrating how they can be used for interacting with MySQL.

    If you wish to find out how this will be done, don’t miss the next part!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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