I. Summary this article briefly discusses the Proxy mode and details how to implement dynamic Proxy in PHP4. This article only provides a prototype of the implementation method. if you have any comments or suggestions, please send them to Binzy [BinzyatJustDNdotCom
I. Summary
This article briefly discusses the Proxy mode and details how to implement dynamic Proxy in PHP4. This article only provides a prototype of the implementation method. due to its limited degree, please submit your comments and suggestions to Binzy [Binzy at JustDN dot Com].
II. Overview
Before we begin to implement Dynamic Proxy, 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 patterns prefixed by "GoF, the Proxy's goal is "Provide a surrogate or placeholder for another object to control access to it (to Provide a Proxy for other objects to control the visit to this object )". common Proxy modes include Remote Proxy, Virtual Proxy, Protection Proxy, and Smart Proxy ).
However, the disadvantage of application 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, then you will invent it. you can easily implement it in PHP4.
III. implementation
PHP4 is a descriptive 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 implementation strategy in this article is 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 it into the 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, which is continuously from IReadFileClass. because PHP4 has no interface, the interface here is imitated. In fact, it is feasible to not apply the implementation 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 ()
{
= Fopen('test.txt ', 'r ');
= Fread (, filesize('test.txt '));
Fclose ();
Return;
}
}
OK. Now we want to verify the user's efficacy, that is, the methods in ReadFileClass participate in protection control. if you create a proxy manually, you can continue to ReadFileClass or implement IReadFileClass, and participate in the protection code (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.
Please refer to the ReadFileClassProxy code in listing 3 and pay attention to the continuous self-ProxyInvocationHandler class.