我扩展了
PHP的MysqLi类,工作正常.但是,如何在查询时使其返回自定义结果对象(或插入/更新/删除等的布尔值)?
原文链接:https://www.f2er.com/php/139171.htmlnamespace MyApp; class MysqLi extends \MysqLi { public function query($query,$resultmode = null) { // This needs to return a MysqLiResult or a boolean } } class MysqLiResult extends \MysqLi_result { }
这样做我可以返回一个MysqLiResult对象,但我无法弄清楚如何为非基于选择的查询返回一个布尔值:
public function query($query,$resultmode = null) { $this->real_query($query); return new MysqLiResult($this); }
更新:
这是我最终使用的:
class MysqLi extends \MysqLi { public function query($query,$resultmode = null) { $result = parent::query($query,$resultmode); return is_bool($result) ? $result : new MysqLiResult($result); } } class MysqLiResult { private $result; public function __construct(MysqLi_result $result) { $this->result = $result; } public function __call($name,$arguments) { return call_user_func_array(array($this->result,$name),$arguments); } public function __set($name,$value) { $this->result->$name = $value; } public function __get($name) { return $this->result->$name; } }