Welcome Guest, Not a member yet? Register   Sign In
Is this a legit practice for a controller
#1

[eluser]uprise78[/eluser]
I am curious is there are any pitfalls to using the PHP5 magic __construct and __destruct to throw my header and footer into the view. As far as I can tell, the Output class handles it all correctly but I figure I would check in to be sure.

Code:
class Test extends Controller {
    
    function __construct()
    {
        parent::Controller();
        $this->load->view('Main/Header');
    }

    ... other stuff here ...
    
    function __destruct()
    {
        $this->load->view('Main/Footer');
    }

}
#2

[eluser]Tom Glover[/eluser]
As far as I know this is correct!
#3

[eluser]Pygon[/eluser]
As a matter of opinion, relying on the PHP shutdown sequence to introduce your footer into the page is a bad idea. Also, again as a matter of opinion, you should really be relying on your view to handle view related material.

In practice -- you can do this, however realize that should you redirect a request to a seperate controller, you've now processed __construct twice (two headers) and registered two shutdown functions (__destruct).

Personally, I would either:

1) Move Header/Footer to the view structure.

2) Handle them as usual in a procedural fashion once you've determined what will be displayed.

You can do it however you wish though.
#4

[eluser]uprise78[/eluser]
Good point Pygon. It's always good to prepare for those just in case scenarios. Header/Footer officially moved out of __construct __destruct.

Thanks for the opinion.
#5

[eluser]Pygon[/eluser]
That's good in my opinion.

__destruct is unreliable at best in my opinion. All you really know that is *at some point and in no particular order* during the shutdown process, it's *probably* going to be run.




Theme © iAndrew 2016 - Forum software by © MyBB