PHP: "method doesn't exist" but it does
I have this strange error,
When I call $element_attrs = $element -> attributes();
, I get a notification that the attribute method does not exist:
Call to undefined method stdClass::attributes();
now when i call die( get_class( $element ) );
Right before the call attributes()
php returns Select_Element
which is correct!
Form_Element
contains a method attribute();
.
I'm pretty sure Select_Element
extends Form_Element
and both files are included indefinitely.
HOWEVER
if i call:
if ( method_exists($element, "attributes") ) {
$element_attrs = $element -> attributes();
}
IT WORKS! method_exists returns true and it is called attributes()
! but when i delete the command if
i get the error 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 here's the code being called in the class Form
:
// note: $this -> elements
is an array of objectsForm_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
}
}
}
}
Does anyone know why PHP thinks it attributes();
doesn't exist? Even if it method_exists($element, "attributes");
returns true
?
source to share
You say you are in a loop with this.
Chances are, the code you are showing is called twice, once when it $element
is the desired object and once when you use it method_exists()
, the code goes past that point and if you don’t use it, it crashes.
When you use die()
, the loop ends at the first item. But it is not necessarily the element that is causing the problem.
Error message
Call to undefined method stdClass::attributes();
supports this: pay attention to stdClass
where it should read Form_Element
.
So, you need to figure out why is $element
not always the object you want.
source to share
You are probably writing die()
after the first step of your loop, but got an error in other steps.
Change die( get_class( $element ) );
to print( get_class( $element ).'<br/>' );
and you will see which line you got the error on and it will probably be empty in that line attribute.
source to share