尝试调用构造函数中的方法时发生致命错误


Fatal error while trying to call a method in constructor

我正在尝试使用本教程编写自己的MVC框架。一切都很顺利,我顺利完成了教程。

然而,后来我决定使用PDO(PHP数据对象)而不是mysql()函数进行数据库操作。所以我修改了我的sqlQuery.php文件,使用mysql函数的PDO istead。

sqlQuery.php

<?php

class SQLQuery {
    private $_dbHandle;
    private $_result;
    /**
     * Connects to database
     *
     * @param $address
     * @param $account
     * @param $pwd
     * @param $name
     */
    function connect($address, $account, $pwd, $name) {
        try{
            $this->_dbHandle = new PDO("mysql:host=$address;dbname=$name", $account, $pwd);
        }catch (PDOException $e) {
            die($e->getCode() . " : " . $e->getMessage());
        }
    }
    /** Disconnects from database **/
    function disconnect() {
        $this->_dbHandle = null;
    }
    function get($whereClause = array()) {
        $query = "select * from $this->_table";
        if(is_array($whereClause) && count($whereClause)){
            $query .= " where ";
            foreach($whereClause as $column=>$value)
                $query .= " $column  = $value";
        }else if(is_int($whereClause)){
            $query .= " where id = $whereClause ";
        }
        return $this->query($query);
    }

    /**
     * Custom SQL Query
     *
     * @param $query
     * @return array|bool
     */
    function query($query) {
        $this->_result = $this->_dbHandle->query($query);
        $this->_result->setFetchMode(PDO::FETCH_CLASS, $this->_model);
        if (preg_match("/select/i",$query)) {
            $result = array();
            $numOfFields = $this->_result->rowCount();
            if($numOfFields > 1){
                while($result[] = $this->_result->fetch()){
                }
            }else{
                $result = $this->_result->fetch();
            }
            return $result;
        }
        return true;
    }
}

现在,当我在我的控制器中打印$this->Item->get()时,我会将数据库中的所有结果作为项目模型的对象,$this->Item->get(2)会按预期为我提供id为2的项目对象。

然而,我不喜欢我的API需要调用一个额外的方法get()来获取项目模型的对象,相反,当项目模型初始化时,我会得到所需的对象,因此我的API可以是$this->item->mycolumnName,这更有意义。

为了实现这一点,我尝试在Models构造函数中移动get()调用,如下所示:

model.php

<?php
class Model extends SQLQuery{
    protected $_model;
    function __construct() {
        $this->connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
        $this->_model = get_class($this);
        $this->_table = strtolower($this->_model)."s";
        $this->get();
    }
    function __destruct() {
    }
}

然而,这给了我一个致命的错误

 Fatal error: Maximum function nesting level of '256' reached, aborting! in /var/www/html/FitternityAssignment/library/sqlquery.php on line 59

我不知道我做错了什么。

问题是第59行:while($result[] = $this->_result->fetch()){

function query($query) {
    $this->_result = $this->_dbHandle->query($query);
    $this->_result->setFetchMode(PDO::FETCH_CLASS, $this->_model);
    if (preg_match("/select/i",$query)) {
        $result = array();
        $numOfFields = $this->_result->rowCount();
        if($numOfFields > 1){
            while($result[] = $this->_result->fetch()){
            }
        }else{
            $result = $this->_result->fetch();
        }
        return $result;
    }
    return true;
}

让我们把话题缩小:

while($result[] = $this->_result->fetch()){
}

无限循环。

fetch()返回某个值时,它被添加到$result,因为$result是一个数组,然后它当然会计算为true,因为每次添加提取的结果时,$result的大小都会增加。

$results = [];
while($row = $this->_result->fetch()){
    $results[] = $row; // or whatever you need to do with the row
}

也许行得通。我说可能是因为这取决于当没有结果时$this->_result->fetch()返回什么。我将假设为false或null,在这种情况下,以上内容将起作用,因为当没有更多结果时,fetch()将返回null或false,然后$row将计算为false。