Home arrow PHP arrow Page 3 - An Introduction to Building Proxy Classes with PHP 5

Expanding the functionality of the ProxyXMLProcessor class - PHP

If you create object-oriented programs in in PHP 5, you know that their performance can be improved by rationalizing the use of objects. The proxy pattern can help with this task. In this first of a two-part series, you will learn the key points of how to use this pattern with PHP 5.

TABLE OF CONTENTS:
  1. An Introduction to Building Proxy Classes with PHP 5
  2. Developing an expandable XML processor class
  3. Expanding the functionality of the ProxyXMLProcessor class
  4. Defining the XMLProcessor class
  5. Seeing the proxy class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
January 02, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the section that you just read, my main purpose here is to extend the functionality of the already familiar proxy class, in addition to implementing its "createXMLProcessor()" method. Of course, the method will be responsible for the instantiation of XML processor objects only when its presence will be required, in this way sticking to the definition of the proxy pattern.

Having said that, below I listed an improved version of the prior proxy class, this time including some additional methods for counting the number of nodes of an input XML string. This new incarnation of this class looks like this:

// define 'ProxyXMLProcessor' class (proxy class for XMLProcessor) class ProxyXMLProcessor{ private $XMLProcessor=NULL; private $xmlstr; // XMLProcessor is not created here by the constructor public function __construct($xmlstr){ $this->xmlstr=$xmlstr; } // display nodes public function displayNodes($node){ // XMLProcessor object is only instantiated when any
of its methods is called if($this->XMLProcessor==NULL){                  $this->createXMLProcessor();                 }                 return $this->XMLProcessor->displayNodes($node); } // fetch nodes as array of objects public function fetchNodesAsObjects(){ if($this->XMLProcessor==NULL){                  $this->createXMLProcessor();                 } return $this->XMLProcessor->fetchNodesAsObjects();         } // count number of nodes contained into XML string public function countNodes(){ if($this->XMLProcessor==NULL){ $this->createXMLProcessor(); }                 return $this->XMLProcessor->countNodes();       } // instantiate XMLProcessor object private function createXMLProcessor(){ $xmlObj=new XMLProcessor($this->xmlstr); $this->XMLProcessor=$xmlObj->getXMLProcessor(); } }

Aside from implementing a new method for counting the nodes of a given XML string, the above class also exposes a private method called "createXMLProcessor." In this case, the method in question is tasked with instantiating an XML processor object when this condition is demanded. That was pretty simple, wasn't it?

Okay, at this stage, I'm pretty certain that you understand how the previous proxy class does its business. However, I have to admit that there's still a missing piece in this puzzle that remains undefined, since you don't know how the corresponding "XMLProcessor" class looks.

In order to address this issue and dissipate any possible doubts, in the next few lines I'm going to show you the respective signature for this XML processing class. Go ahead and read the next section.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: