观察者模式

<?php
class user{
    public $loginnum;
    public $ab;

    public function __construct($loginnum,$ab){
        $this->loginnum=$loginnum;
        $this->ab=$ab;
    }

    public function guancha($key,$val){
        $this->obj[$key]=$val;
    }


    public function zhixing(){
        if($this->obj){
            foreach ($this->obj as $key => $value) {
                $value->update($this);
            }
        }
    }
}


class ab{
    public function update($obj){
        var_dump($obj->ab);
    }
}

class loginnum{
    public function update($obj){
        var_dump($obj->loginnum);
    }
}

$user = new user(1,5);
$user->guancha('ab',new ab());
$user->guancha('loginnum',new loginnum());
$user->zhixing();

 

posted @ 2019-06-16 22:05  酷酷的城池  阅读(107)  评论(0编辑  收藏  举报