doue2666 2012-03-23 10:46
浏览 104
已采纳

PHP:'方法不存在',但它确实存在

I have this strange error,

When I call $element_attrs = $element -> attributes(); I get a notice saying the attribute method does not exist:

Call to undefined method stdClass::attributes();

now when I call die( get_class( $element ) ); Right before the attributes() call, php returns Select_Element which is correct!

Form_Element contains the attribute(); method.

I am positive Select_Element extends Form_Element and both files are included indefinately.

HOWEVER

if I call:

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

IT WORKS! method_exists returns true, and attributes() is called! but when I remove the if command, I get the error notice again...

What the hell is going on!

CODE

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
    }
}

and this is where the code is called in a Form class :

// note: $this -> elements is an array of Form_Element objects

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
            }
        }
    }
}

Does anybody know why PHP thinks attributes(); doesnt exist? Even though method_exists($element, "attributes"); returns true ?

  • 写回答

2条回答 默认 最新

  • duanfu3884 2012-03-23 10:48
    关注

    You say you are in a loop with this.

    Most likely, the code you show is called twice, once with $element being the desired object, and once not - when you use method_exists(), the code walks past that point, and if you don't use it, it crashes.

    When you use die(), the loop terminates at the first element. But that is not necessarily the element that is causing the problem.

    The error message

    Call to undefined method stdClass::attributes();
    

    supports this: note the stdClass where it should read Form_Element.

    So you need to find out why $element is not always the object you want it to be.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端