An Introduction to Building Proxy Classes with PHP 5 - Seeing the proxy class in action
(Page 5 of 5 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |