标签:
在一些框架中经常看到对sql语句的封装,比如插入一条sql。
$userDB->email = "lee@163.com"; $userDB->address = "xxxx"; $userDB->save(); //=======或者======== $data=array("email" => "lee@163.com", "address" = "xxxx"); $userDB->save($data);
下边是对这种封装的一个简单思路,只是思路哦!
class UserDB extends DB { const INSERT_SQL = "insert into users(email, address) valus(?, ?)"; private $_data; //接收分别添加的字段 private $_fields = array(‘email‘, ‘address‘); //过滤白名单 public function save($data = array()) { $insertData = empty($data) ? $this->_data : $data; $this->db->query(self::INSERT_SQL, $insertData); } public function __set($key, $value) { if (!in_array($key, $this->_fields)) { throw new InvalidArgumentException("$key is not allow"); } $this->_data[$key] = $value; } }
标签:
原文地址:http://www.cnblogs.com/leezhxing/p/4936407.html