PHP Development School: PHP P4 implements dynamic proxy _ PHP Tutorial

Source: Internet
Author: User
PHP Development School: implements dynamic proxy in PHP4. Overview before we start to implement DynamicProxy, we should first understand what Proxy is and what it is used for. The following is a good image from the blog Hall Garfield P 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.

Before implementing Dynamic Proxy, maybe we should first understand what Proxy is and what it is used. the following is a good image of Garfield from the blog Hall...

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.