_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 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!'; } } }
Auth类是一个进行权限验证的类, 此处我们只是简单的查看传入的UserName, 如果是Binzy, 那么自然是可以看秘密的J, 如果是Jasmin, 那么HoHo, 没得看, 给Binzy点空间嘛.:D 详见清单四.
清单四 class Auth { function Auth() { } // bool // function CheckAuth($username) { if ($username == 'Binzy') { return true; } return false; } }
Ok, 下面我们来使用我们创建的代理. 请见清单五.
清单五 require_once('ReadFileClass.php'); require_once('ReadFileClassProxy.php');
$proxy = ReadFileClassProxy::NewInstance(new ReadFileClass()); print $proxy->ReadMyFile();
结果如下: 如果是Binzy, 那么自然可以知道那个秘密.
如果是Jasmin, 这个秘密当然不能让她知道.
五、 总结 代理是一个非常有用的模式. PHP4虽然并不是真正的Object-Oriented, 但仍然可以实现你想实现的设计. 写本文的目的有很大部分是希望国内PHP开发者不要再拘泥于现在的开发现状, 开发出更好的PHP软件. 而不是一 上一页 [1] [2] [3] 下一页 |