CodeIgniter Forums
Calling parent's constructor within a controller function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Calling parent's constructor within a controller function (/showthread.php?tid=48482)



Calling parent's constructor within a controller function - El Forum - 01-18-2012

[eluser]Unknown[/eluser]
I came across have the following code written by previous developers

class Mall_admin extends MY_Controller {
function __construct() {
parent::__construct();
}

function example(){
parent::__construct();
......
}
}

I'm not sure why they've decided to call the parent's constructor again within the function (I can't think of any reason). Would it be safe for me to delete that one line since the parent's constructor should be called within the class's constructor?

Thanks for any help


Calling parent's constructor within a controller function - El Forum - 01-18-2012

[eluser]Silviu[/eluser]
I was not aware that such practice is even possible. Proof of how forgiving PHP can be.

Back to your question. My take on the situation:

Calling the constructor there (assuming it works, since the object is already initialized) has the (intended) purpose of re-initializing some of the variables. This should obviously be done by defining another class method and calling it whenever needed.

1. Check out the parent constructor. What happens there?
2. Comment out the parent::__construct() call. Are there pages that are handled by that class that behave differently?
3. Get back with more info after several test Wink


Calling parent's constructor within a controller function - El Forum - 01-18-2012

[eluser]Matt S.[/eluser]
It should be safe to take out the parent::__construct() call from the other methods, since it's a bit redundant to have them in there. Whenever a class gets initialized, the __construct() method always gets called first. In an MVC pattern, as is CodeIgniter, this will happen on every HTTP request to your controller methods.