Welcome Guest, Not a member yet? Register   Sign In
Calling parent's constructor within a controller function
#1

[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
#2

[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
#3

[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.




Theme © iAndrew 2016 - Forum software by © MyBB