Blogger Information
Blog 61
fans 0
comment 0
visits 53934
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
属性和方法的重载
笑颜常开的博客
Original
814 people have browsed it

<?php
//重载:动态地创建类属性和方法
//重载的实现手段:通过魔术方法
//一、属性重载
//const IS_ISSET=true;
//const IS_GET=true;
//const IS_SET=true;
//const IS_UNSET=false;
//class Visit{
//    protected $data=[];
//    public function __get($name)
//    {
//        // TODO: Implement __get() method.
//        return IS_GET ? $this->data[$name]:'非法访问';
//    }
//    public function __set($name,$value)
//    {
//        // TODO: Implement __get() method.
//        return IS_SET ? $this->data[$name]=$value:'非法访问';
//    }
//}
//$visit=new Visit();
//if(isset($visit->table)){
//    echo $visit->table,'<br>';
//}else{
//    $visit->table='table_staff';
//}
////访问
//echo $visit->table.'<br>';
////更新
//$visit->table='table_foods';
////访问
//echo $visit->table,'<br>';
//unset($visit->table);
require 'inc/Site.php';
class Web{
   public function __call($name, $arguments)
   {
       // TODO: Implement __call() method.
//        方法重载更多地用于跨类的方法调用上
   return call_user_func_array([(new Site),'show'],$arguments);
   }
   public static function __callStatic($name, $arguments)
   {
       // TODO: Implement __callStatic() method.
//    跨类调用一个静态方法
   return call_user_func_array(['Site','add'],$arguments);
   }
}
$web=new Web();
//访问一个不存在的方法
echo $web->show('php中文网','海量资源,公益免费'),"<hr>";
//访问一个不存在的静态方法
echo Web::add(30,50);

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