PHP
  Home arrow PHP arrow Page 2 - An Introduction to Building Proxy Classes with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

An Introduction to Building Proxy Classes with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-01-02


    Table of Contents:
  • An Introduction to Building Proxy Classes with PHP 5
  • Developing an expandable XML processor class
  • Expanding the functionality of the ProxyXMLProcessor class
  • Defining the XMLProcessor class
  • Seeing the proxy class in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    An Introduction to Building Proxy Classes with PHP 5 - Developing an expandable XML processor class
    ( Page 2 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT