美文网首页
魔术方法(拦截器)

魔术方法(拦截器)

作者: 幻无虚生 | 来源:发表于2019-07-18 14:34 被阅读0次
    <?php
    class PersonWrite{
    
        function writeName(Person $p){
            print $p->getName()."\n";
        }
    
    
        function writeAge(Person $p){
            print $p->getAge()."\n";
        }
    
    }
    
    
    
    class Person{
        private $writer;
    
        public function __construct(PersonWrite $writer){
                $this->writer=$writer;
        }
    
        public function __call($mothod,$args){
            if(method_exists($this->writer,$mothod)){
                return $this->writer->$mothod($this);
            }
        }
    
    
        function getName(){
            return "Bob";
        }
    
        function getAge(){
            return "44";
        }
    
        public function __toString()
        {
    
           return $this->getAge();
        }
    
    }
    
    
    $person=new Person(new PersonWrite());
    
    $person->writeName();

    相关文章

      网友评论

          本文标题:魔术方法(拦截器)

          本文链接:https://www.haomeiwen.com/subject/dirllctx.html