<?php
class Printer
{
public function printSth($str)
{
echo "第一步 :之前先把 $str[0] 打残. <br/><br/>";
echo "第二步:然后打120把 $str[0] 送进医院.<br/><br/>";
echo "第三步:然后出院,然后再执行第一步.";
}
//..............
}
class TextShop //这是一个文印处理店,只文印,卖纸,不照相
{
private $printer;
public function __construct(Printer $printer)
{
$this->printer=$printer;
}
public function sellPaper() //卖纸
{
echo 'give you some paper <br/>';
}
public function __call($method,$args) //将代理对象有的功能交给代理对象处理
{
if(method_exists($this->printer, $method)) {
$this->printer->$method($args);
}
}
}
class PhotoShop //这是一个照相店,只文印,拍照,不卖纸
{
private $printer;
public function __construct(Printer $printer) {
$this->printer = $printer;
}
public function takePhotos() { //照相
echo 'take photos for you <br>';
}
public function __call($method, $args) { //将代理对象有的功能交给代理对象处理
if(method_exists($this->printer, $method)) {
$this->printer->$method($args);
}
}
}
$printer = new Printer();
$textShop = new TextShop($printer);
$photoShop = new PhotoShop($printer);
$textShop->printSth('小张');
php静态代理的实例
最新推荐文章于 2020-08-17 14:44:07 发布