call方法

PHP中的__call()魔术方法2

一世执手 提交于 2019-12-09 14:20:15
相信大家在用ThinkPHP开源框架的时候,对Model类的连贯操作方法肯定很感兴趣吧,那今天我们就用PHP中的__call()魔术方法去实现对SQL语句select的查询的连贯操作。 代码: <? header("Content-type:text/html;charset=utf-8"); class testcall{ public $p; /*记录SQL关键字*/ /*select * from table where id=1 order by id limit 1,2*/ function __call($method_name,$arg){ if(in_array($method_name,array('field','table','where','order','limit'))){ $this->p[$method_name]=$arg[0]; } return $this; /*返回当前对象 testcall*/ } function select(){ $field=isset($this->p['field'])?$this->p['field']:'*'; $table=isset($this->p['table'])?$this->p['table']:'default'; $where=isset($this->p['where'])?' where