php 代理模式
代理模式为其他对象提供一种代理以控制对这个对象的访问。
Irequest.php
<?php namespace proxy; interface Irequest{ function sendRequest(); }
Request.php
<?php namespace proxy; class Request{ function sendRequest(){ echo "send Request"; } }
RequestProxy.php
<?php namespace proxy; class RequestProxy{ private $_request; function __construct(){ $this->_request = new Request(); } function sendRequest(){ $this->beforeRequest(); echo "<br>"; $this->_request->sendRequest(); echo "<br>"; $this->afterRequest(); } protected function beforeRequest(){ echo "before"; } protected function afterRequest(){ echo "after"; } }
client.php
<?php namespace proxy; require '../autoload.php'; $request = new RequestProxy(); $request->sendRequest();
autoload.php
<?php spl_autoload_register(function ($class_name) { $file_dir = str_replace('\\','/',$class_name); require_once $file_dir . '.php'; });
client.php为客户端发起请求的文件,autoload.php为自动加载类的文件