This question builds upon the previous question I asked which is similar in nature This question is a two part question concerning the way html is out put to the screen, based on the class bellow.
Based on the answer to that question I created the following:
class Form{
protected $_html = '';
public function __construct(){
$this->init();
}
public function init(){
}
public function open_form(){
$this->_html .= '<form>';
}
public function elements(){
$this->_html .= 'element';
}
public function close_form(){
$this->_html .= '</form>';
}
public function create_form(){
$this->open_form();
$this->elements();
$this->close_form();
}
public function __toString(){
return $this->_html;
}
}
The problem with this class is that if I do:
$form = new Form
echo $form->create_form();
Then nothing gets printed out. How ever if I change create_form to do the following:
public function create_form(){
$this->open_form();
$this->elements();
$this->close_form();
echo $this->_html;
}
Then it works and I see:
<form>elements</form>
Why is this? and how would I fix it?
The second part to this question is that I have a function, and no I cannot change the out put of this function that echoes hidden fields into a form. The problem is if I do:
public function create_form(){
$this->open_form();
$this->elements();
function_echoes_hidden_fields();
$this->close_form();
echo $this->_html;
}
// Sample function to echo hidden fields.
public function function_echoes_hidden_fields(){
echo "hidden fields";
}
I now see:
"hidden fields"
<form>elements</form>
How would you fix this problem?
What I know is that return, returns a value for further processing, meaning you have to echo out the function that returns the value if you want to display that value, while echo will escape processing imediatly and echo out the value.
My problem is, I am trying to use them together, to create a form.
call create_form and then echo the class.
or add
return $this->_html;to create_form() and your existing code will work.