_php tutorial on implementing dynamic Proxy in PHP4

Source: Internet
Author: User
Tags php software
Author: Binzy Source: Beyond PHP

I. Summary
This paper simply expounds the proxy mode and concretely explains how to implement dynamic Agent in PHP4, this paper just gives a prototype of the method of implementation. Due to the limited level, there are any comments and suggestions please feedback to Binzy [binzy at justdn Dot Com].

Ii. Overview
Before we start implementing the dynamic proxy, perhaps we should first understand what a proxy is and what it does. Here is a good image from the blog Hall Garfield to tell about the proxy article: "Wudang Apprenticeship and caching agent." Proxy mode is one of the 23 design patterns introduced by "GoF", and the purpose of proxy is "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 agent (virtual proxy), protection agent (Protection proxy), intelligent agent (smart proxy).
But one disadvantage of using proxies is that you have to manually create a copy of the proxy class that you need (that is, the proxy class). This means that if you create a virtual Proxy for the image class, you will have to manually create a Imageproxy class that has the same method as the image class. Ok, if you are as lazy as I am, you will have to think of the dynamic to generate proxy. Yes, then you'll find that it's easy to implement it in PHP4.

Third, the realization
Since PHP4 is an interpreted language, it is weakly typed and has no interfaces. So in the implementation of the time there are both convenient and inappropriate. This article is only one of the implementation methods.
The strategy implemented in this article is actually very simple. The core is the Proxyfactory class and the Clazz class, proxyfactory is responsible for instantiating clazz and assigning values. The Clazz class is responsible for creating and returning proxies. Creating a proxy is done in the form of a temporary file.
For details, see the code in the proxyfactory.php and clazz.php two files. Do not repeat here.
In addition, we define a Proxyinvocationhandler class in proxyinvocationhandler.php.

Iv. examples
We now have a Readfileclass class, which inherits from the Ireadfileclass, because PHP4 has no interface, so the interface is simulated, in fact, in PHP4 do not use implementation interface is also feasible J. For the specific contents of the two classes, see clear single and listing two.

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

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

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

OK, we're now going to add the ability to authenticate users by adding protection controls to the methods in Readfileclass. If you create a proxy manually, you can inherit readfileclass or implement Ireadfileclass and add the Protection code (in fact, it is free in PHP4 because it is object-_-except for the basic type). But let's try to create a proxy by experimenting with the dynamic agent we just implemented.

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

List III
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's 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, here we simply look at the incoming username, if it is binzy, then nature can see the secret J, if it is Jasmin, then HoHo, no see, give Binzy point space.:D See listing four.

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


Ok, let's use the agent we created. See listing Five.

List 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 were Jasmin, the secret would certainly not let her know.

V. Summary
The proxy is a very useful pattern. PHP4 is not really a object-oriented, but it can still achieve the design you want to achieve. The purpose of writing this article is a lot of hope that the domestic PHP developers do not adhere to the current development situation, the development of better PHP software. Rather than a pile of script stacks.

http://www.bkjia.com/PHPjc/314716.html www.bkjia.com true http://www.bkjia.com/PHPjc/314716.html techarticle Author: Binzy Source: Beyond PHP One, abstract this paper briefly expounds the proxy mode and concretely explains how to implement dynamic Agent in PHP4, this article just gives a prototype of the method of realization ...

  • 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.