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()

有没有朋友遇到过的 敬请指教

附言 1  ·  1年前

使用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;
    }
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 22

使用 get_class 找到对应的类,然后直接查找有没有这个方法,还能看如何处理的

1年前 评论
xiao (楼主) 1年前
xiao (楼主) 1年前

这个问题,你可以查下报错的代码,有些代码网页直接复制过去用是会报错的,需要格式化下

1年前 评论
deatil (作者) 1年前
xiao (楼主) 1年前

select('*') 这个方法执行后 return $this ,就可以接着调用fetchAll

1年前 评论
xiao (楼主) 1年前
xiao (楼主) 1年前

看一下 from() 返回啥。

1年前 评论
xiao (楼主) 1年前
xiao (楼主) 1年前

楼主只贴出报错信息,是报错在哪一行代码定位了吗?又是否是这里报错呢?

Laravel

1年前 评论
xiao (楼主) 1年前
秦晓武 1年前
php_yt (作者) 1年前

$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()不是一个好的编码习惯,建议哈~

1年前 评论
xiao (楼主) 1年前
# 看源码猜测应该是
 public function fetchAll($keyField = '')
    {
        $stmt = $this->query();    // 结果是 null 
        if(empty($keyField)) return $stmt->fetchAll();    // null->fetchAll()
        $rows = array();
        while($row = $stmt->fetch()) $rows[$row->$keyField] = $row;
        return $rows;
    }
1年前 评论
xiao (楼主) 1年前
xiao (楼主) 1年前
xiao (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!