get_class_methods($object)显示存在fetchAll方法,但是$object->fetchAll()却报错?难道这是php的bug
1. 运行环境
win10 + PHP Version 5.4.19 + apache2.4
2.软件环境
使用的是 zentao 8.1.3 的一个软件管理系统
3. 问题描述
进行db查询的时候报错如下
查询代码:
$this->dao->select('*')->from('zt_structrue_task_dispatch')->fetchAll()
报错
Fatal error: Call to a member function fetchAll() on a non-object
并且报错的时候,我用下列代码判断是否存在该方法,显示是存在fetchAll方法的
$query = $this->dao->select('*')->from('zt_structrue_task_dispatch');
if (in_array('fetchAll',get_class_methods($query))){
dd($query->fetchAll());
}
但是是下列的查询则不会报错
$this->dao->select('id')->from('zt_structrue_task_dispatch')->fetchAll()
$this->dao->select('*')->from('zt_user')->fetchAll()
有没有朋友遇到过的 敬请指教
使用get_class 显示类名为dao
然后在dao类下 能查到fetchAll 方法,代码如下
/**
* 获取所有记录。
* Fetch all records.
*
* @param string $keyField 返回以该字段做键的记录
* the key field, thus the return records is keyed by this field
* @access public
* @return array the records
*/
public function fetchAll($keyField = '')
{
$stmt = $this->query();
if(empty($keyField)) return $stmt->fetchAll();
$rows = array();
while($row = $stmt->fetch()) $rows[$row->$keyField] = $row;
return $rows;
}
使用 get_class 找到对应的类,然后直接查找有没有这个方法,还能看如何处理的
这个问题,你可以查下报错的代码,有些代码网页直接复制过去用是会报错的,需要格式化下
select('*') 这个方法执行后 return $this ,就可以接着调用fetchAll
看一下
from()
返回啥。楼主只贴出报错信息,是报错在哪一行代码定位了吗?又是否是这里报错呢?
$this->dao->select('')->from('zt_structrue_task_dispatch')->fetchAll() 报错 Fatal error: Call to a member function fetchAll() on a non-object 正常情况是$this->dao->select('')->from('zt_structrue_task_dispatch')这里会返回一个对象,然后在这个对象上调用fetchAll()方法,报的错说明前面那个没有正确返回一个对象,所以是一个non-object,当然不能调用fetchAll()方法,结合楼主下面的$this->dao->select('id')->from('zt_structrue_task_dispatch')->fetchAll()这句不会报错,应该排查一下为啥select('')的时候会出错是什么原因,另外,select('')加fetchAll()不是一个好的编码习惯,建议哈~