0

I have a series of functions that is suppose to, in the end create a form. they reside in a class such as the following example:

class ExampleForm{ protected $_html = ''; protected $_element; public function __construct($element){ $this->_element = $element; protected function _open_form(){ echo $this->_html .= '<form>'; } public function create_form(){ $this->_open_form() $this->_html .= $this->_element; echo "some content"; $this->_close_form() } protected function _close_form(){ echo $this->_html .= '</form>' } public function __toString(){ return $this->_html; } 

Instantiated as:

$element = 'some element'; new ExampleForm($element); 

A more comprehensive example can be seen on Github on line 86 this content gets echoed out, How ever its echoed out side the form.

If you want to see a live action demo of this Then you'll deff wan't me. at the end of the day if you inspect the form on the web page, you'll see above the form opening tag two hidden fields that need to be inside the form tags.

Now one would think this would create a form such as:

<form> element some conent </form> 

How ever, what I get is:

some content <form> element </form> 

So can some one tell me, based on what I have above why I am getting my "some content" echoed out side the form tag even though I have echoed the opening form tag?

even if I do not echo the opening form tag the "some content" still appears out side the opening form tag.

4
  • now add the code calling the class and its methods Commented Jan 14, 2013 at 19:21
  • Please post all of the code you are using to output this. You need to show us how/where the functions inside the class are being called. Commented Jan 14, 2013 at 19:24
  • How you are instantiating your class and calling those functions? Please provide code. Commented Jan 14, 2013 at 19:27
  • I have added examples. Because I cannot post more then two links I hope what I gave was enough Commented Jan 14, 2013 at 20:02

2 Answers 2

3

echo performs output immediately, e.g.

function bar() { echo 'bar'; } bar(); echo 'foo'; 

gives barfoo as the output. But if you had

function bar() { return 'bar'; } $baz = bar(); echo 'foo'; echo $baz; 

you'll get foobar

Sign up to request clarification or add additional context in comments.

1 Comment

So what should I do in my case to make sure that "some content" is staying inside the tags?
1

Return will simply return the variable without echoing it, where as echo will echo it out when it is called. So if you return a variable and then echo something. So in your function create_form(), instead of echo "some content"; just do $this->_html .= "some content";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.