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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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