Blogger Information
Blog 17
fans 0
comment 0
visits 12606
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
静态绑定与拦截器学习心得
越努力越幸运
Original
749 people have browsed it

//说明:基类和调用者都有:say()方法,默认指向基类的那个say();运行结果应该显示:'1'

//当基类中使用了:return static::say()后,指向为调用者的那个say();运行结果应该显示:'2'

     class Eee{

         public static function say(){

              echo '1<br>';

         }

    

         public static function beCaller(){

              return static::say();

         }

     }

    

     class Fff extends Eee{

         public static function say(){

              echo '2<br>';

         }

     }

    

     Fff::say();

//私有属性的访问和修改

     class Ggg{

         private $aa;

         private $bb;

        

         public function __construct($cc,$dd){

             $this->aa=$cc;

             $this->bb=$dd;

         }

    

         public function __get($ee){

             $method='get'.ucfirst($ee);

             if(method_exists($this, $method)){

                  return $this->$method();

             }else {

                  return null;

             }

         }

    

         public function getName(){

              return $this->aa.'<br>';

         }

    

         public function getPrice(){

              return $this->bb.'<br>';

         }

    

         public function __set($property,$value){

             $method='set'.ucfirst($property);

             if(method_exists($this, $method)){

                  return $this->$method($value);

             }else {

                  return null;

             }

         }

    

         private function setAa($value){

              $this->aa=trim($value);

         }

    

         private function setBb($value){

              $this->bb=$value;

         }

    

     }

    

     $ggg=new Ggg('初值', 1000);

     echo $ggg->aa;

     echo $ggg->bb;

    

     $ggg->aa='重写值';

     $ggg->bb=2000;

    

     echo '<hr>';

     echo $ggg->aa;

     echo $ggg->bb;

//方法拦截器

     class Hhh{

         public function __call($ff,$gg){

              printf('方法名:%s,参数:[%s]',$ff,implode(',',$gg));

          }

     }

    

     $hhh=new Hhh();

     $hhh->demo(1,2,3,4);

     echo '<br>';


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post