HomePHP Page 4 - An Introduction to Building Proxy Classes with PHP 5
Defining the XMLProcessor 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.
In consonance with the concepts related to the proxy pattern that you learned before, it's necessary to define the signature of the corresponding "XMLProcessor" class. This will help you understand how a potential proxy object can access the methods that belong to different XML processors.
This being said, below I included the signature for the pertinent "XMLProcessor" class. You'll quickly realize that it performs some useful tasks on a given XML string. Therefore, have a look at this brand new class, please:
// define 'XMLProcessor' class
class XMLProcessor{
private $sxmle;
public function __construct($xmlstr){
if(!$this->sxmle=new SimpleXMLElement($xmlstr)){
throw new Exception('Error reading XLML
string');
}
}
// display values of a given node
public function displayNodes($node){
$nodes='';
foreach($this->sxmle as $obj){
$nodes.='Value of '.$node.' node is the
following : '.$obj->$node.'<br />';
}
return $nodes;
}
// fetch all nodes as array of objects
public function fetchNodesAsObjects(){
$nodes=array();
foreach($this->sxmle as $node){
$nodes[]=$node;
}
return $nodes;
}
// count number of nodes contained into XML string
public function countNodes(){
$nodeCounter=0;
foreach($this->sxmle as $node){
$nodeCounter++;
}
return $nodeCounter;
}
// get 'SimpleXMLElement' object
public function getXMLProcessor(){
return $this;
}
}
As shown above, the "XMLProcessor" class has been provided with a wealth of handy methods which can be used for counting and displaying the different nodes of a specified XML string, in addition to fetching the complete set of XML data as an array of objects. Though all the methods that I just described play an important role in the context of the XML processing class in question, I'd like you to focus your attention on the definition of the "getXMLProccessor()" method.
As you can see, the only task assigned to the referenced method consists of simply returning to calling code an instance of the corresponding XMLProcessor" class. Logically, this condition allows a proxy class to access all the methods that belong to the respective XML processor, something that perfectly suits the requirements established by the proxy pattern.
All right, at this level I hope that you grasped the key points on how the previous XML processing class does its thing, since its signature is indeed pretty straightforward. Thus, bearing this condition in mind, the only thing that remains undone is developing an illustrative example that will show you how the XML proxy class and its respective target object can work in tandem to process a simple XML string.
Naturally, the example will be shown in the following section, thus to see how it will be created, please click on the link that appears below and keep reading.