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 patterns introduced by "GoF". The Proxy aims to "Provide a surrogate or placeholder for another object to control access (a favorite of small websites) 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 php (as the mainstream development language) 4 is easy to implement.
Implementation
Because php (as the mainstream development language) 4 is interpreted language, weak type, and has no interface. therefore, there are both convenient and inappropriate implementations. 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 details, see the code in ProxyFactory. php (as the mainstream development language) and Clazz. php (as the mainstream development language.
In addition, in ProxyInvocationHandler. php (as the mainstream development language), we define a ProxyInvocationHandler class.
Example
We now have a ReadFileClass class that inherits from IReadFileClass. Because php (as the mainstream development language) 4 has no interfaces, the interfaces here are simulated, in fact, php (as the mainstream development language) 4 does not use the implementation interface is also feasible J. 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, php (as the mainstream development language) 4 is very free, 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 (as the mainstream development language ));
Require_once (ProxyInvocationHandler. php (as the mainstream development language ));
Require_once (Auth. php (as the mainstream development language ));
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.