【问题标题】:How to set mysqli_result::num_rows如何设置 mysqli_result::num_rows
【发布时间】:2016-11-03 22:20:16
【问题描述】:

出于模拟目的,我扩展了 mysqli_result 类:

class MysqliResultMock extends mysqli_result {
   ...
}

现在,我需要覆盖它为名为“num_rows”的成员返回的内容,但不幸的是 $this->num_rows = 123;它根本无法设置(我在写这一行时遇到了一个致命错误:

Fatal error: Cannot directly set the property MysqliResultMock::$num_rows in DbTest.php on line 13)

可以通过某种方式完成吗?

谢谢。

【问题讨论】:

    标签: php unit-testing mocking tdd hhvm


    【解决方案1】:

    哈哈,我想出了答案。

    您只需要添加公共 $num_rows;明确地添加到类中,使其可写。像这样:

    类结果扩展 mysqli_result {

    public $num_rows;
    
    public function __construct()
    {
        $this->num_rows = 6;
    

    ...

    然后您可以在实例上调用 mysql_num_rows(),它会正确返回给定的 num_rows。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 2017-03-03
      相关资源
      最近更新 更多