<?php
/**
* 利用魔术方法__call模拟数据库连贯操作
*/
class DB
{
public $sql=array(
'field'=>'',
'where'=>'',
'order'=>'',
'limit'=>''
);
function __call($name,$args){
$name=strtolower($name);
if(array_key_exists($name,$this->sql)){
$this->sql[$name]=$args[0];
}else{
echo 'Class'.get_class().' do not have a method '.$name;
}
return $this;
}
function select(){
echo "SELECT ".$this->sql['field']." FROM user ".$this->sql['where']." ".
$this->sql['order']." ".$this->sql['limit'];
}
}
$obj =new DB();
$obj->field("id,name,time")
->where("where id>9")
->order('order by id')
->limit(10)
->select();
?>利用魔术方法__call模拟数据库连贯操作
最新推荐文章于 2020-11-21 03:18:49 发布
本文介绍如何使用PHP魔术方法__call来实现数据库查询的连贯操作,通过设置类属性实现SQL参数传递,最终输出SQL查询语句。
5107

被折叠的 条评论
为什么被折叠?



