PHP
  Home arrow PHP arrow Page 4 - 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 - Building an array processing factory
    ( Page 4 of 5 )

    If you’ve been reading some of my PHP-related articles, published here on the prestigious Developer Shed network, then you’ll know that I’m a strong advocate of using hands-on examples for reaffirming concepts that are part of the unavoidable theory. For that reason, I'm going to show you another case where static methods and properties can be used together inside the same PHP application.

    In short, let me explain the basics of the example that I plan to build: first off, I’ll create a few array handling classes, aimed at processing array elements in different ways. Then, with these classes well underway, I’ll define a generic array processing factory class, which will use a couple of static methods and properties to “fabricate” each of the array handling classes that I mentioned before.

    Since this example seems pretty promising, I’ll begin by listing the set of array handling classes that I plan to use here. Below are the respective signatures of these classes:

    // define abstract 'ArrayProcessor' class abstract class ArrayProcessor{ private $arrayFile; private $elements=array(); abstract function addArrayElement($element); abstract function processArray(); abstract function saveArray(); abstract function getArray(); } // define 'ArrayToUppercase' class class ArrayToUppercase extends ArrayProcessor{ public function__construct($arrayFile=
    'default_path/array_data.txt') {if(!file_exists($arrayFile)) { throw newException
    ('Invalid array file!'); }        $this->arrayFile=$arrayFile; } public function addArrayElement($element){ if(!$element) { throw new Exception
    ('Invalid array element! Must be a non-empty value.'); }       $this->elements[]=$element; } // convert array elements to uppercase public function processArray(){        $this->elements=array_map
    ('strtoupper',$this->elements); } // save array elements to file public function saveArray(){       if(!$fp=fopen($this->arrayFile,'a+')){ throw new Exception
    ('Error opening array file');}        if(!fwrite($fp,serialize($this->elements))){ throw new Exception
    ('Error writing data to file');} fclose($fp); } // get array of elements public function getArray(){ return $this->elements; } // define 'ArrayToLowercase' class class ArrayToLowercase extends ArrayProcessor{ public function __construct
    ($arrayFile='default_path/array_data.txt'){         if(!file_exists($arrayFile)){ throw new Exception
    ('Invalid array file!'); }       $this->arrayFile=$arrayFile; } public function addArrayElement($element){ if(!$element) throw new Exception
    ('Invalid array element! Must be a non-empty value.'); }      $this->elements[]=$element; } // convert array elements to lowercase public function processArray(){        $this->elements=array_map
    ('strtolower',$this->elements); } // save array elements to file public function saveArray(){      if(!$fp=fopen($this->arrayFile,'a+')){ throw new Exception
    ('Error opening array file'); }     if(!fwrite($fp,serialize($this->elements))){ throw new Exception
    ('Error writing data to file'); } fclose($fp); } // get array of elements public function getArray(){ return $this->elements; } } // define 'ArrayToReverse'class class ArrayToReverse extends ArrayProcessor{ public function __construct
    ($arrayFile='default_path/array_data.txt') {        if(!file_exists($arrayFile)) { throw new Exception
    ('Invalid array file!'); }       $this->arrayFile=$arrayFile; } public function addArrayElement($element){ if(!$element){ throw new Exception
    ('Invalid array element! Must be a non-empty value.'); }         $this->elements[]=$element; } // reverse array elements public function processArray(){        $this->elements=array_reverse($this->elements); } // save array elements to file public function saveArray(){        if(!$fp=fopen($this->arrayFile,'a+')){ throw new Exception
    ('Error opening array file'); }         if(!fwrite($fp,serialize($this->elements))){ throw new Exception
    ('Error writing data to file'); } fclose($fp); } // get array of elements public function getArray(){ return $this->elements; } }

    All right, after proceeding further, let me explain briefly what I’ve done until now. As you can see, first I defined an abstract base “ArrayProcessor” class. From this class I derived three different child classes, aimed at performing different operations on the corresponding array elements, such as lowercasing, uppercasing, and finally reversing them.

    As you saw, all the classes that were created before have a “save()” method, which allows you to save the corresponding array elements to a given text file. The logic that drives these classes should be extremely easy to grasp.

    At this point, and after defining the previous array processing classes, I’m sure you’re wondering... where do static properties and methods fit into this scenario? Well, I’m glad you asked! Because in the following section, I’ll be defining an array processor factory class, which not surprisingly will use the so-called static methods and members.

    If you’re interested in learning how this factory class will look, please keep  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 2 Hosted by Hostway
    Stay green...Green IT