dosin84644 2018-04-11 14:39
浏览 51

使用mysqli_result :: fetch_object和我自己的类不起作用

I'll try to use fetch_object('Classname') to test this method.

The Class Inserent in Inserent.php:

namespace classes\model;

 class Inserent{
    private $nummer;
    private $nickname;
    private $email;

    public function __construct ($nummer, $nickname, $email){
        $this->nummer = $nummer;
        $this->nickname = $nickname;
        $this->email = $email;
    }

    public function getNummer(){
        return $this->nummer;
    }

    public function setNummer($nummer){
        $this->nummer = $nummer;
    }
 ...
 }

The use is in a Mapper-Class InserentDAO.php:

namespace classes\mapper;
use classes\model\Inserent;

class InserentDAO{
    private $dbConnect;

    public function __construct (){
        $this->dbConnect = MySQLDatabase::getInstance();
    }

    public function readAll(){
        $sql = "SELECT inserentennummer, nickname, email FROM inserent";
        $inserent = null;
        $inserentList = array();

        if ($result = $this->dbConnect->query($sql)) {

            while ($inserent = $result->fetch_object('classes\model\Inserent')) {
                $inserentList[] = $inserent;
            }
            $result->close();
        }
        return $inserentList;
    }
...
}

I get errors and my Objects are empty:

Warning: Missing argument 1 for classes\model\Inserent::__construct() in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 10

Warning: Missing argument 2 for classes\model\Inserent::__construct() in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 10

Warning: Missing argument 3 for classes\model\Inserent::__construct() in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 10

Notice: Undefined variable: nummer in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 11

Notice: Undefined variable: nickname in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 12

Notice: Undefined variable: email in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 13

But if I change the code, in the Mapper, it will works.

part of the code InserentDAO.php instead:

while ($obj = $result->fetch_object()) {
     $inserent = new Inserent($obj->inserentennummer, $obj->nickname, $obj->email);
     $inserentList[] = $inserent;
}
  • 写回答

2条回答 默认 最新

  • doukuang8166 2018-04-11 14:48
    关注

    I believe fetch_object invokes an empty constructor and then sets the properties according to result columns it fetched. There is a comment on the manual confirming that.

    So this is what fetch_object("classes\model\Inserent") is doing:

    $object = new classes\model\Inserent();
    $object->inserentennummer = $result['inserentennumer'];
    $object->nickname = $result['nickname'];
    $object->email = $result['email'];
    return $object;
    

    This is why it's complaining about missing parameters and undefined variables (they're private).

    The obvious solution is to refactor your class to provide an empty constructor and public properties:

    namespace classes\model;
    
    class Inserent{
        public $nummer;
        public $nickname;
        public $email;
    
        public function __construct ($nummer = 0, $nickname = "", $email = ""){
            //...
    

    An alternative to making the properties public would be to use the __set magic method, but you'd still have to provide a zero-parameter constructor.

    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口