【问题标题】:PHP: 'method doesnt exist', but it doesPHP:'方法不存在',但确实存在
【发布时间】:2012-03-23 10:46:01
【问题描述】:

我有这个奇怪的错误,

当我调用$element_attrs = $element -> attributes(); 时,我收到一条通知说属性方法不存在:

Call to undefined method stdClass::attributes();

现在当我在attributes() 调用之前调用die( get_class( $element ) ); 时,php 返回Select_Element,这是正确的!

Form_Element 包含attribute(); 方法。

我很肯定 Select_Element 扩展了 Form_Element 并且这两个文件都被不确定地包含在内。

但是

如果我打电话:

if ( method_exists($element, "attributes") ) {
    $element_attrs = $element -> attributes();
}

它有效! method_exists 返回 true,并调用 attributes()!但是当我删除if 命令时,我再次收到错误通知......

这到底是怎么回事!

代码

interface Element{
    public function __construct( $element );
    public function parse();
}

class Form_Element implements Element{

    protected $element;

    public function __construct($json_element){
        $this -> element = $json_element;
    }

    public function parse(){
        // Removed parsing code, unrelated
    }

    ... removed unrelated methods ...

    public function attributes( $key = null, $value = null ){
        if ( is_null( $key ) ){
            return $this -> element -> attributes;
        }
        else{
            $this -> element -> attributes -> $key = $value;
        }
    }
}

class Select_Element extends Form_Element implements Element{

    public function __construct( $element ) {
        parent::__construct( $element );
    }

    public function parse(){
        // Removed parsing code, unrelated
    }
}

这是在 Form 类中调用代码的地方:

// 注意:$this -> elementsForm_Element 对象的数组

public function edit_form($name_of_element, $name_of_value, $value){
    foreach ( $this -> elements as $element ){
        if ( method_exists($element, "attributes") ) {
            $element_attrs = $element -> attributes();
        }
        if ( $element_attrs -> name == $name_of_element ){
            switch ( $name_of_value ){
                case "selected" :
                    $element -> selected( $value );
                    break;
                case "options" :
                    $element -> options( $value );
                    break;
                case "value" :
                    $element -> value( $value );
                    break;
                // add more support as needed
            }
        }
    }
}

有人知道为什么 PHP 认为 attributes(); 不存在吗?即使method_exists($element, "attributes"); 返回true ?

【问题讨论】:

  • 您调用该方法的对象是stdClass(这是反序列化 json 或将数组转换为对象时通常会得到的),因此它显然不是您的 Form_Element 类的实例.
  • @ThiefMaster 但他说get_class( $element ) 返回正确的类型。
  • 您确定您的elementForm_Element 的实例吗?
  • 运算符是否有空格?应该是这样的$element->attributes();
  • @Truth 我认为这里的简单答案是他处于循环中,并且die() 不需要使用导致问题的元素停止循环。我敢打赌没有涉及 PHP 错误。

标签: php oop


【解决方案1】:

你说你在这个循环中。

很可能,您显示的代码被调用了两次,一次是 $element 是所需的对象,一次不是 - 当您使用 method_exists() 时,代码会经过该点,如果您不使用它,它崩溃了。

当您使用die() 时,循环在第一个元素处终止。但这不一定是导致问题的元素。

错误信息

Call to undefined method stdClass::attributes();

支持这一点:注意stdClass,它应该读作Form_Element

所以你需要找出为什么$element 并不总是你想要的对象。

【讨论】:

  • 你是对的,我不敢相信我没有意识到我正在经历一个循环-.-
【解决方案2】:

您可能在循环的第一步之后写了die(),但在其他步骤中出错了。

die( get_class( $element ) ); 更改为print( get_class( $element ).'<br/>' );,您会看到在哪一行出现错误,并且该行的属性可能为空。

【讨论】:

    猜你喜欢
    • 2018-07-15
    • 2012-01-24
    • 1970-01-01
    • 2014-06-20
    • 2015-09-25
    • 1970-01-01
    • 2018-08-02
    • 2017-10-19
    • 2017-05-26
    相关资源
    最近更新 更多