您的位置:首页 > 编程语言 > PHP开发

16.php代理模式

2016-01-22 09:52 731 查看
对原对象进行继承并重写要被改变的模块,这就是代理模式。

<?php
class CD{
    protected $name;
    protected $price;
    protected $num;
    
    function __construct($name, $price, $num){
        $this->name = $name;
        $this->price = $price;
        $this->num = $num;
    }
    
    function buy(){
        echo $this->_pay().',已经完成了购买!';
    }
    
    function _pay(){
        return "使用农业银行支付!";        
    }
}

/* 
 * 新的支付手段出现我们要切换到其他支付手段
 *  */
class Buyproxy extends CD{
    function _pay(){
        return "使用支付宝进行支付!";
    }
}

$buy = new BuyProxy("《乌云背后的幸福线》", "43$", "3");
$buy->buy();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: