Jump to content

OOP - echo inside a class


johnsmith153

Recommended Posts

This probably doesn't matter much, but I am using a 'Display' class for final output to the browser.

 

The class will display the obvious HTML header and then display either the full site or the mobile site.

 

It also displays the CSS / JS (which is previously selected in the page-specific controller code as there are variations based on server-side checks).

 

Basically it is the ONLY class that actually needs to send anything to the browser.

 

When I begin output and echo inside the class, that is the end of the script - there is no more server-side code to execute.

 

If I shouldn;t echo inside the class, is it that bad to do so?

Link to comment
Share on other sites

I think it's fine to echo inside the class if the purpose of the class is to echo your page.  The Smarty display method is an example of this.

 

But within the class I would recommend that you generate the entire output in a string, and only echo it with a single statement at the end.  IE your display method would be

 

function display() {
    $output = $this->generate();
    echo $output;
}

 

And no other method from this class (and all other classes) would echo anything.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.