Dynamic agent in PHP4

zhaozj2021-02-16  55

Dynamic agent in PHP4

First, summary

This paper briefly explains the Proxy mode and specifically explains how to implement a dynamic agent in PHP4. This article only gives the prototype of the implementation method. Because of the limited level, any comments and suggestions please feedback to binzy [binzy At Justdn Dot COM] .

Second, overview

Before we started Dynamic Proxy, maybe we should first understand what proxy and it use. Below is a

The nice image of the Garfourd Garfield Tells Proxy's article: "

Wudang Academic and Cache Agent ". Proxy mode is one of the 23 design modes introduced by" GOF ". The purpose of Proxy is" Provide A Surrogate or PlaceHolder for Another Object to Control Access To IT "provides a proxy for other objects Control the access to this object) ". Generally common proxy mode: Remote Proxy, Virtual Proxy, Protection Proxy, Smart Proxy.

But using the agent has a bad place to manually create a copy (ie, proxy class). This means if you create a Virtual Proxy for the Image class, then you have to create a way with the image class with the same Method ImageProxy class. OK, if you are as lazy as me, you will think of dynamics to generate proxy. Yes, you will find it, in actually you can easily implement it.

Third, realize

Since php4 is an interpreted language, weak type, and no interface. So it is only convenient to achieve it when it is realized. This article is only one of the implementation methods.

The strategy implemented in this paper is actually very simple. The core is the ProxyFactory class and the CLAZZ class, and ProxyFactory is responsible for instanting CLAZZ and assigns a value. Create Proxy is created and returned by the CLAZZ class. Creating Proxy is in a temporary file.

Please check the code in proxyfactory.php and clazz.php and Clazz.php. This is not described again.

In addition, we define a proxyInvocationHandler class in ProxyInvocationHandler.php.

Fourth, example

We now have a ReadFileClass class, which inherits from IreadFileClass, because PHP4 does not interface, so the interface is analog, in fact, it is also possible to use the implementation interface in PHP4, and the specific content of the two classes, please see And list two.

List one

Class IReadFileClass

{

Function readmyfile () {}

}

List of two

Class ReadFileClass Extends IreadfileClass

{

Function readmyfile ()

{

$ fp = fopen ('Test.txt', "R");

$ DATA = FREAD ($ FP, FileSize ('Test.txt');

Fclose ($ fp);

Return $ DATA;

}

}

OK, we now give to the function of verify the user, add protection control to the method in ReadFileClass. If you use manual creation agents, then you can inherit readFileClass or implement iReadFileClass, and join protection code (actually in PHP4 is very free, because In addition to basic types of object -_-). However, we now try to create proxy with dynamic agents just implemented.

Please see the list of ReadFileClassProxy of the list, note that the class inherits from the ProxyInvocationHandler class.

List of three

Require_once ('proxyFactory.php');

Require_once ('proxyinvocationhandler.php');

Require_once ('author.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 buy here, but it is useful.

Function Invoke (& $ PROXY, $ Method, $ Parameters)

{

$ uname = 'binzy';

// $ uname = 'jasmin';

IF (Auth :: CheckAuth)

{

Return Parent :: Invoke (& $ Proxy, $ Method, $ Parameters);

}

Else

{

//

Return 'no permission!';

}

}

}

The Auth class is a class that performs permission verification. We are just simple to view incoming username. If it is binzy, then naturally you can see secret J, if it is jasmin, then Hoho, did not see, give binzy point space Well: D. See the list four.

List of four

Class Auth

{

Function auth ()

{

}

// bool

//

Function Checkauth ($ Username)

{

IF ($ usrname == 'binzy')

{

Return True;

}

Return False;

}

}

OK, let's use the agent we created. Please see the list.

List of 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 you can know that secret J.

If it is jasmin, this secret can certainly can't let her know.

Five, summary

The agent is a very useful mode. Although PHP4 is not true Object-Oriented, you can still achieve the design you want to realize. There is a large part of the purpose of writing this article, I hope that domestic PHP developers should not stick to the current development status Develop better PHP software. Instead of being a bunch of Script accumulation.

Six, thank you

Thank you

Freeman tests me.

thank

Mmkk's Code Formatter HTC.

Seven, reference

1.

Gof

2. GOF Chinese translation

3. PHP reference manual

http://www.php.net/manual/en/

Eight, related downloads

Original code:

Http://0926.net/bbs/attachment.php?aid=127

转载请注明原文地址:https://www.9cbs.com/read-23681.html

New Post(0)