我正在尝试编写自己的 MVC 框架 using this tutorial .一切正常,我顺利完成了教程。
但是后来我决定使用 PDO(PHP 数据对象)而不是 mysql() 函数来进行数据库操作。所以我修改了我的 sqlQuery.php 文件以使用 PDO 代替 mysql 函数。
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;
}
}
现在,当我在 Controller 中打印 $this->Item->get()
时,我将数据库中的所有结果作为项目模型的对象和 $this->Item ->get(2)
按预期给我 id=2 的项目对象。
但是,我不喜欢这样的想法,即我的 API 需要调用一个额外的方法 get() 来获取项目模型的对象,相反,当一个项目模型被初始化时,我得到所需的对象,因此我的 API 可以是 $this->item->mycolumnName
。
为了实现这一点,我尝试像这样在模型构造函数中移动 get()
调用:
模型.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
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
将然后评估为假。
关于php - 尝试在构造函数中调用方法时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291529/