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

Seeing the proxy class in action - 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 accordance with the concepts that I deployed in the prior section, the simplest way to understand how the corresponding "ProxyXMLProcessor" class works to access all the methods of a potential XML processor object rests on setting up a comprehensive example. In this example, a simple XML string will be processed by using the proxy pattern.

For this specific case, say you have the following sample XML string in a file called "xmlstr.php:"

<?php $xmlstr=<<<XML <?xml version="1.0" encoding="iso-8859-1"?> <users> <user> <name>John Doe</name> <location>Location 1</location> <email>john@domain.com</email> </user> <user> <name>Mary Shelley</name> <location>Location 2</location> <email>mary@domain.com</email> </user> <user> <name>Peter Parker</name> <location>Location 3</location> <email>peter@domain.com</email> </user> <user> <name>Susan Norton</name> <location>Location 4</location> <email>susan@domain.com</email> </user> <user> <name>Steve Powell</name> <location>Location 5</location> <email>steve@domain.com</email> </user> </users> XML; ?>

Now, after listing the above XML string, it's possible to use the previous XML proxy class in the following way:

try{ // include XML string require_once 'xmlstr.php'; // instantiate 'ProxyXMLProcessor' object $pxmlproc=new ProxyXMLProcessor($xmlstr); // display number of 'email' nodes contained into XML string echo $pxmlproc->displayNodes('email'); /* displays the following:            Value of email node is the following : john@domain.com Value of email node is the following : mary@domain.com Value of email node is the following : peter@domain.com Value of email node is the following : susan@domain.com Value of email node is the following : steve@domain.com */          // fetch nodes as array of objects echo $pxmlproc->fetchNodesAsObjects(); /* displays the following: array */ // count number of nodes contained into XML string             echo $pxmlproc->countNodes(); /*           displays the following: 5 */ } catch(Exception $e){ echo $e->getMessage(); exit(); }

As you can see, the previous example shows clearly how the corresponding "XMLProxyProcessor" can be used to count, display and fetch all the nodes contained in the XML string that you saw before, by using all the methods exposed by an XML processor object.

After studying the above example, you'll agree with me that building proxy classes with PHP 5 is indeed a no-brainer process, right?

Wrapping up

We've come to the end of this first part of the series. Aside from introducing the core concepts for how to implement the proxy pattern with PHP 5, I provided you with some examples that may help you understand more easily how this pattern works.

In the final article of this series, I'll show you how to use this handy pattern to construct a proxy object for the "DirectoryIterator" class that comes bundled with PHP 5. So, stay tuned until the next part!  



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