So I wrote a basic class which I have extended to create html element. Based on Zend – HOWEVER not exactly. No this is not a question about or in relation to zend
class AisisCore_Form_Elements_Input extends AisisCore_Form_Element {
protected $_html = '';
public function init(){
foreach($this->_options as $options){
$this->_html .= '<input type="text" ';
if(isset($options['id'])){
$this->_html .= 'id="'.$options['id'].'" ';
}
if(isset($options['class'])){
$this->_html .= 'class="'.$options['class'].'" ';
}
if(isset($options['attributes'])){
foreach($options['attributes'] as $attrib){
$this->_html .= $attrib;
}
}
$this->_html .= $this->_disabled;
$this->_html .= ' />';
return $this->_html;
}
}
}
so this class extends my element class which consists of a constructor that takes in an array of options, a basic element is set up as such:
$array_attrib = array(
'attributes' => array(
'placeholder' => 'Test'
)
);
$element = new AisisCore_Form_Elements_Input($array_attrib);
echo $element;
So what’s the problem?
echoing the $element object gives me an error saying it cant convert the object to a string, thus when I var_dump it I get this back:
object(AisisCore_Form_Elements_Input)#21 (3) {
["_html":protected]=>
string(22) "<input type="text" />"
["_options":protected]=>
array(1) {
["attributes"]=>
array(1) {
["placeholder"]=>
string(4) "Test"
}
}
["_disabled":protected]=>
NULL
}
Can some one explain what’s going on? Last I checked I was echoing out a string not an object. How did I manage to create an object?
If you need to see the AisisCore_Form_Element class I will post it how ever all this class is a base class you extend to create the element. the only thing it takes is an array of options.
Looks like your constructor was trying to return a value (in the middle of the for loop as well), when you probably wanted to something like this….
Now your example can be updated to look like this…