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


       · 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 4 hosted by Hostway
    Stay green...Green IT