【问题标题】:PHP: call a function from another function (out of scope?)PHP:从另一个函数调用一个函数(超出范围?)
【发布时间】:2012-07-02 15:39:21
【问题描述】:

我在通过 nuSOAP 创建 Web 服务时遇到问题(尽管我相信我的问题与此无关)

我想做什么:

function loadActiveItems() {
    $list = Item::loadActive();
    $ret = array();
    foreach ($list as $val){
        //two tests to check if i really have an object and if the toDTO method is callable
        echo var_dump($val);
        echo is_callable(array($val, 'toDTO'));
        array_push($ret, $val->toDTO());
    }
    unset($val);
    return $ret;
}

我收到以下错误:

Call to a member function toDTO() on a non-object

并且var_dump($val)is_callable 都从我在网上看到的返回预期(分别为对象和真),看来我有一个超出范围的问题......但出于某种原因我我似乎没有理解它:P

提前致谢

编辑:好吧,只要检查一下,显然我也不明白 is_callable 因为我总是得到 1 作为结果...... EDIT2:如果有任何帮助,我正在使用 php-activerecord

【问题讨论】:

  • 您不应回显 var_dump() 的结果。你能发布 var_dump 的结果吗?
  • 也许可以试试foreach($list as &$val) { ... }
  • @Florent object(Item)[25] public 'errors' => null private 'attributes' (ActiveRecord\Model) => array (size=5) 'id' => int 1 'itemtype_id' => int 1 'parent_id' => null 'name' => string 'item1' (length=5) 'active' => int 1 private '__dirty' (ActiveRecord\Model) => array (size=0) empty private '__readonly' (ActiveRecord\Model) => boolean false private '__relationships' (ActiveRecord\Model) => array (size=0) empty private '__new_record' (ActiveRecord\Model) => boolean false
  • @Greg nope... 这不是静态的 =( public function toDTO(){ require_once 'DTO/ItemDTO.php'; return new ItemDTO($this->id, $this-> name, null, $this->itemtype->name, $this->parent->toDTO()); }
  • @TomIngram 也没有用 =/ 还是谢谢!

标签: php oop function scope nusoap


【解决方案1】:

toDTO() 可能在您的班级 Item 中未定义。

另一个原因可能是该方法不是公共的或如@Grep 所说的那样是静态的。

【讨论】:

  • ` public function toDTO(){ require_once 'DTO/ItemDTO.php'; return new ItemDTO($this->id, $this->name, null, $this->itemtype->name, $this->parent->toDTO()); }` 不幸的是没有 =/
  • get_class_methods($val) 的结果是什么?
  • 嗯,一个巨大的数组 :P 但12 => string 'toDTO' (length=5) 在那里
  • 我的问题可能很愚蠢,但是你确定所有 $val 都不为空吗?
  • 刚刚按照 Mihau Stancu 的建议进行了检查,没有任何空值 =/
【解决方案2】:

这个错误永远不会发生在定义方法但它是静态或受保护/私有的对象上:

Call to a member function toDTO() on a non-object

仅当$val 不是对象时才会发生该错误。通常是 NULL、FALSE 或其他标量。

当对象为 db_fetch() 函数而来但之前的获取或查询失败时,通常为 FALSE。

当你有一个可能包含 NULL 的数组时,它通常是一个 NULL。

var_dump($list) 看看里面有什么以及是否有任何 NULL。还将您的 foreach 更改为具有 $key 和 var_dump($key) 以及查看在发出错误之前最后转储的密钥。

【讨论】:

  • 列表没有任何 NULL 值并且第一项发生错误 ($key = 0) =/
  • 那个键的值是多少?
  • object(Item)[25] public 'errors' => null private 'attributes' (ActiveRecord\Model) => array (size=5) 'id' => int 1 'itemtype_id' => int 1 'parent_id' => null 'name' => string 'item1' (length=5) 'active' => int 1 private '__dirty' (ActiveRecord\Model) => array (size=0) empty private '__readonly' (ActiveRecord\Model) => boolean false private '__relationships' (ActiveRecord\Model) => array (size=0) empty private '__new_record' (ActiveRecord\Model) => boolean false
  • 为错误打印debug_backtrace。也许错误不是来自该对象本身,而是来自堆栈中的函数调用。
  • 好吧,我不是真正的 php 专家,所以我尝试阅读 debug_backtrace(这是巨大的 XD)我没有看到对 $val 变量的引用,并且根据我发现我应该有,对吧?
【解决方案3】:

好的,我发现了问题...感谢所有帮助! 我在 toDTO 中调用另一个对象的 toDTO ...问题是该对象可能为空! 所以一个简单的 if(object==null) 就解决了这个问题!

再次感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    相关资源
    最近更新 更多