实例
<?php include 'call_user_func_array.php'; class Demo2 { public static function __callStatic ($name, $arguments) { return call_user_func_array([(new Select()),$name],$arguments ); } } $res=Demo2::table('student')->where('id<7')->fields('*')->find(); $table='<table align="center" cellspacing="0" cellpadding="3" border="1px,red">'; $table.='<caption>学生信息</caption>'; $table.='<tr bgcolor="aqua"><th>id</th><th>姓名</th><th>性别</th><th>年纪</th><th>班级</th></tr>'; foreach ($res as $current) $table.='<tr><td>'.$current['id'].'</td><td>'.$current['name'].'</td><td>'.$current['gender'].'</td><td>'.$current['age'].'</td><td>'.$current['class'].'</td></tr>'; $table.='</table>'; echo $table;
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php class Select { private $pdo; private $table; private $fields; private $where; public function __construct() { $this->pdo=new PDO("mysql:host=127.0.0.1;dbname=php",'root',''); $this->pdo->query("SET NAMES utf8"); } public function table($arguments) { $this->table=$arguments; return $this; } public function fields($arguments) { $this->fields=$arguments; return $this; } public function where($arguments) { $this->where=$arguments; return $this; } public function find() { $sql="select $this->fields from $this->table where $this->where"; $stmt=$this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
方法重载
__call($method,$arguments) 当用户访问一个不存在或者无权限的方法时调用
__callStatic()当用户访问一个不存在或者无权限的静态方法时调用
call_user_func_array(函数||方法,参数数组 )
在类中 call_user_func_aray([对象,'方法'],参数数组) 方法用字符串,对象new 类名
对于静态的方法, call_user_func_array(['对象','方法'],[参数数组]) 对象方法都是字符串
将 static 想像成一个变量: 始终指向静态方法的调用者