Implementing dynamic Proxy in PHP4

Source: Internet
Author: User
Tags add auth object implement interface php and php software access
Dynamic One, summary
This paper simply expounds the proxy model and illustrates how to implement the dynamic proxy in PHP4, this paper only gives a prototype of the method. Because the level is limited, have any comments and suggestions please feedback to Binzy [binzy at justdn Dot Com].

Ii. Overview
Before we start implementing dynamic Proxy, maybe we should take a look at what a proxy is and what it does. Here is a good image from the blog Hall Garfield tells the proxy article: "Wudang Apprentice and caching agent." Proxy mode is one of the 23 design patterns introduced by "GoF", which is intended to "provide a surrogate or placeholder for another object to control access to it" For an agent to control access to this object) ". Common proxy modes are: Remote proxy, virtual proxy (virtual proxies), protection agent (Protection proxy), intelligent agent (smart proxy).
But the disadvantage of using a proxy is that you have to manually create a copy of the proxy class you need (that is, the proxy class). This means that if you create a virtual Proxy for the image class, you have to manually create a Imageproxy class that has the same method as the image class. Ok, if you're as lazy as I am, you're sure to think of dynamic to generate proxy. Yes, the next thing you'll find out is that you can easily implement it in PHP4.

Third, realize
Because PHP4 is an interpreted language, it is weakly typed and has no interfaces. So in the realization of the time there are both convenient and there is no place. Here is not rigidly adhere to the implementation method, this article is just one of the implementation methods.
The strategy implemented in this article is actually very simple. The core is Proxyfactory class and Clazz class, proxyfactory is responsible for instantiating clazz and assigning values. The Clazz class is responsible for creating and returning a proxy. Proxy creation is done in the form of a temporary file.
See the code in the proxyfactory.php and clazz.php two files. No more details here.
In addition, we define a Proxyinvocationhandler class in proxyinvocationhandler.php.

Iv. examples
We now have a Readfileclass class that inherits from Ireadfileclass, because PHP4 has no interface, so the interface here is analog, and it's actually not possible to use the implementation interface in PHP4. J. See single and listing two for details of two classes.

Clear single
Class Ireadfileclass
{
function Readmyfile () {}
}



Listing II
Class Readfileclass extends Ireadfileclass
{
function Readmyfile ()
{

$fp = fopen (' Test.txt ', "R");
$data = Fread ($fp, FileSize (' test.txt '));
Fclose ($FP);
return $data;
}
}



OK, now we're going to add the function of verifying the user, that is, to add protection control to the method in Readfileclass. If you are creating a proxy manually, you can inherit readfileclass or implement Ireadfileclass, and add protection code (in fact, it is quite free in PHP4, because all but the basic types are object-_-). But let's try the dynamic proxy we just implemented to create a proxy.

Take a look at the Readfileclassproxy code in listing three, and note that the class inherits from the Proxyinvocationhandler class.

Listing three
Require_once (' proxyfactory.php ');
Require_once (' proxyinvocationhandler.php ');
Require_once (' auth.php ');

Class Readfileclassproxy extends Proxyinvocationhandler
{

var $object;

Function Readfileclassproxy (& $obj)
{
$this->object = & $obj;
}

//
Function newinstance (& $obj)
{
$proxyFactory = Proxyfactoryinstance ();
Return $proxyFactory->create (new Readfileclassproxy (& $obj),
Get_parent_class (& $obj));
}

$proxy is isn't used here, but it is useful.
Function Invoke (& $proxy, $method, $parameters)
{
$uname = ' Binzy ';
$uname = ' Jasmin ';
if (Auth::checkauth ($uname))
{
Return Parent::invoke (& $proxy, $method, $parameters);
}
Else
{
//
Return ' No permission! ';
}
}
}



Auth class is a permission to verify the class, where we simply look at the incoming username, if it is binzy, then nature can see the secret of J, if it is Jasmin, then HoHo, no see, give Binzy space.:D See listing four for details.

Listing four
Class Auth
{
function Auth ()
{
}
bool
//
function Checkauth ($username)
{
if ($username = = ' Binzy ')
{
return true;
}
return false;
}
}


Ok, let's use the proxy we created below. Please see listing Five.

Listing five
Require_once (' readfileclass.php ');
Require_once (' readfileclassproxy.php ');

$proxy = readfileclassproxy::newinstance (New Readfileclass ());
Print $proxy->readmyfile ();



The results are as follows:
If it is binzy, then nature can know the secret.



If it is Jasmin, this secret certainly cannot let her know.



V. Summary
A proxy is a very useful pattern. PHP4 is not really a object-oriented, but it can still achieve the design you want to implement. The purpose of this article is a large part of the hope that the domestic PHP developers do not adhere to the current development situation, to develop a better PHP software. Rather than piling up a bunch of script.

Vi. Thank you.
Thanks Buddy Freeman for testing for me.
Thanks to Mmkk's code Formatter HTC.

Vii. Reference
1. GoF
2. Gof Chinese Version
3. PHP Reference manual http://www.php.net/manual/en/

Eight, related downloads
Related accessories: This article original code



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.