Implementation of dynamic proxy in PHP4 author: Binzy Source: beyond PHP
I. Summary
This article briefly describes the Proxy mode and describes how to implement dynamic Proxy in PHP4. This article only provides a prototype of the implementation method. due to the limited level, please submit any comments or suggestions to Binzy [Binzy at JustDN dot Com].
II. Overview
Before we begin to implement Dynamic Proxy, maybe we should first understand what Proxy is and what it is used. the following is an article about Proxy in the good image of Garfield from the blog: "Wudang Arts and cache Proxy ". the Proxy mode is one of the 23 design modes introduced by "GoF, proxy aims to "Provide a surrogate or placeholder for another object to control access to it (Provide a Proxy for other objects to control access to this object )". common Proxy modes include Remote Proxy, Virtual Proxy, Protection Proxy, and Smart Proxy ).
However, the disadvantage of using a proxy is that you have to manually create a copy of the required proxy class (that is, the proxy class ). this means that if you create a Virtual Proxy for the Image class, you have to manually create an ImageProxy class with the same Method as the Image class. OK. if you are as lazy as you are, you will surely think of dynamic generation of Proxy. yes, you will find that you can easily implement it in PHP4.
III. implementation
PHP4 is an interpreted language, weak type, and has no interface. Therefore, it is both convenient and inappropriate to implement PHP4. the implementation method is not limited here. this article is just one of the implementation methods.
The policy implemented in this article is actually very simple. the core is the ProxyFactory class and Clazz class. ProxyFactory is responsible for instantiating Clazz and assigning values. the Clazz class is responsible for creating and returning the Proxy. the Proxy is created by writing a temporary file.
For more information, see the code in the ProxyFactory. php and Clazz. php files.
In addition, we define a ProxyInvocationHandler class in ProxyInvocationHandler. php.
IV. example
We now have a ReadFileClass class that inherits from IReadFileClass. because PHP4 has no interface, the interface here is simulated. In fact, it is feasible to not use the implemented interface in PHP4. for more information about the two types, see steps 1 and 2.
Listing 1
Class IReadFileClass
{
Function ReadMyFile (){}
}
List 2
Class ReadFileClass extends IReadFileClass
{
Function ReadMyFile ()
{
$ Fp = fopen('test.txt ', "r ");
$ Data = fread ($ fp, filesize('test.txt '));
Fclose ($ fp );
Return $ data;
}
}
OK. Now we want to add the user verification function, that is, add protection control to the methods in ReadFileClass. if you create a proxy manually, you can inherit ReadFileClass or implement IReadFileClass and add protection code (in fact, it is very free in PHP4, because all except the basic types are object -_-). but now we try to use the dynamic Proxy we just implemented to create a Proxy.
See the ReadFileClassProxy code in listing 3. Note that this class inherits from the ProxyInvocationHandler class.
Listing 3
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 not 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! ';
}
}
}
The Auth class is a class for permission verification. here we simply look at the passed UserName. if it is Binzy, we can naturally look at the secret J. if it is Jasmin, then HoHo, you have no idea. just give Binzy some space.: D. for details, see List 4.
Listing 4
Class Auth
{
Function Auth ()
{
}
// Bool
//
Function CheckAuth ($ username)
{
If ($ username = 'binzy ')
{
Return true;
}
Return false;
}
}
OK. here we will use the proxy we created. See listing 5.
Listing 5
Require_once ('readfileclass. php ');
Require_once ('readfileclassproxy. php ');
$ Proxy = ReadFileClassProxy: NewInstance (new ReadFileClass ());
Print $ proxy-> ReadMyFile ();
The result is as follows:
If it is Binzy, you can naturally know the secret.
If it is Jasmin, this secret certainly cannot be known to her.
V. Summary
Proxy is a very useful mode. although PHP4 is not a real Object-Oriented, it can still implement the design you want to implement. most of the purposes of writing this article are to hope that domestic PHP developers will not stick to the current development status and develop better PHP software. instead of a pile of scripts.