Home arrow PHP arrow 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.

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

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.



 
 
>>> 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: