PHP代理模式的实现

 
更多

代理模式是把难复制的、占资源多的、远程速度慢的对象。各自对应一个代理对象,被代理后进行本地的操作。

<?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();

打赏

本文固定链接: https://www.cxy163.net/archives/2646 | 绝缘体

该日志由 绝缘体.. 于 2022年10月04日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: PHP代理模式的实现 | 绝缘体
关键字: , , , ,

PHP代理模式的实现:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter