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 ?
stdClass(that's what you usually get when deserializing json or casting an array to object), so it's clearly not an instance of yourForm_Elementclass.get_class( $element )return the correct type.elementis an instance ofForm_Element?$element->attributes();die()does not necessary stop the loop with the element that is causing the problem. I'm willing to bet there is no PHP bug involved.