一个mysql数据库类 发表于 2015-09-03 | 分类于 PHP日记 | 评论数: | 阅读次数: 本文字数: 999 | 阅读时长 ≈ 2 分钟 12345678910111213141516171819202122232425262728293031323334353637383940<?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(); ?> 打赏 微信支付 支付宝