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

Developing an expandable XML processor 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

Proxy objects can be easily created by defining a simple class that works as an intermediate entity for another one (this resembles the concept of proxy servers). In this case, the proxy is responsible for accessing a class in particular, which will be instantiated only when its functionality is required.

Actually, the above definition seems rather hard to grasp, so let me show you an illustrative example where a proxy class is created to access a generic XML processor only when this object is needed by a specific application.

Having said that, here is the initial signature that corresponds to the mentioned proxy class, which not surprisingly I called "ProxyXMLProcessor." Have a look at the definition for this new class, please:

// 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(); } }

If you take some time and examine the class shown above, then you'll realize that undoubtedly there are some interesting aspects concerning its definition. First, the referenced proxy class really behaves as an intermediate entity for an XML processor object, since all the methods that belong to it are accessed via the proxy. This concept is simple, right?

In addition, you can see that the previous "ProxyXMLProcessor" class also offers a couple of straightforward methods for fetching and displaying the nodes of an inputted XML string. However, one thing that surely caught your attention is the fact that these methods will instantiate the corresponding XML processor object via the respective "createXMLProcessor()" method only when its functionality is required. Period.

Now, do you see why this pattern is aimed at avoiding the unnecessary instantiation of different objects? I hope you do!

Now that you hopefully grasped the logic that stands behind the proxy pattern, let me go one step further and show you how to improve the "ProxyXMLProcessor" class by expanding its existing functionality, in addition to implementing its still undefined "createXMLProcessor" method.

As you might have guessed, all these tasks will be discussed in the following section, thus click on the link 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: