代理模式是把难复制的、占资源多的、远程速度慢的对象。各自对应一个代理对象,被代理后进行本地的操作。
<?php
class Printer{
public function doPrint(){
echo "I can Print!";
}
}
class TakePhoto{
private $printer;
public function __construct($printer){
$this->printer=$printer;
}
public function doPhoto(){
echo "I can takePhoto";
}
public function __call($method,$args="") {
if (method_exists($this->printer, $method)) {
$this->printer->$method($args);
}
}
}
$p=new Printer();
$t=new TakePhoto($p);
$t->doPrint();
微信扫一扫,打赏作者吧~