Welcome Guest, Not a member yet? Register   Sign In
Parent Controller
#1

[eluser]lenwood[/eluser]
I've been watching/reading tutorials for two weeks now, trying to learn as much as I can. I've noticed that when building controllers, some developers use constructors and some don't. I'm brand new to developing with PHP (although I've been a hack for years), what's the significance/relevance of including

Code:
function Something()
       {
            parent::Controller();
       }

Is there a best practice? How do I know if I need this?
#2

[eluser]Jelmer[/eluser]
You need to have the constructor there to properly preserve the CI application flow. If you don't in the case of controllers, you won't have access to all the base classes and any other autoloaded classes.
Not using contstructors as instructed in the User Guide will give you trouble and unexpected behavior.
Constructors can be called the same as their class or just __construct() (in PHP5), both are valid constructors.

If you want to find out exactly how and what you can always just find out by looking into the Controller class in the system/libraries folder and see what happens when you call it's constructor...

[EDIT] I was mistaken, if you don't define a new constructor it'll be inherited - more in posts #5 and #6.
#3

[eluser]lenwood[/eluser]
That makes sense, thanks Jelmer. I assume its the same with models?

It's curious to me that in most of the video tutorials that I've seen (I've been watching Jeff Way's series on nettuts) the constructors are left out.
#4

[eluser]theprodigy[/eluser]
First off, I'm going to say that I never build child classes without a constructor (or any class for that matter), but it is my understanding that constructors act like other functions in the case of inheritance.

If the child does not have a constructor, it inherits the parent's constructor, as long as the parent's constructor is of the proper scoping (public or protected).

So, following that logic, if you build a controller that extends CI's controller class, and you leave the constructor out, it "should" still work fine since your class will inherit the constructor of the base controller class, and thus all the functionality will still be there.

If I am wrong, please correct me, but this is what I understand to be the case.
#5

[eluser]Jelmer[/eluser]
I must admit I'm not entirely sure on this either, but I thought that parent constructors are only inherited as a function - in which case they are only called when named in the PHP5 manner ("__construct"). The PHP4 constructors named after their own class aren't considered constructors by their children I thought (because their name doesn't match the child's name), but I might be mistaken...
#6

[eluser]theprodigy[/eluser]
I may be reading too much into it, but according to PHP's Documentation

Quote: Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

Now, like I said, I may be reading too much into it, but one of the things I noticed was
Quote:Parent constructors are not called implicitly if the child class defines a constructor.

Now, granted, this is PHP5, as it is in their current documentation. But, it also says
Quote:For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class
which says to me, it will look for the __construct() of the child class, then search for the old-style constructor (classname()). If it doesn't find one, by default it looks to the parent. And I would assume it does the same with the parent, then the grandparent, and so on, until it finds a constructor to call, or give up if it reaches the base without a constructor.
#7

[eluser]Jelmer[/eluser]
Yeah you're right. I Googled "PHP4 Manual" and found some mirrors of the old manual. It turns out that what I described was PHP3 behavior, PHP4 does call it's parent's constructor (and as you described PHP5 does as well).

Just for others having researched less: if you do define a child's constructor you do have to call on the parent's constructor, if you don't you'll break CI's app flow. Because as theprodigy quoted:
Quote:Parent constructors are not called implicitly if the child class defines a constructor.
#8

[eluser]theprodigy[/eluser]
of course, in my mind, that still doesn't give enough reason NOT to put a constructor in.

I always put one in even if the only thing I am doing is calling the parent's constructor (mainly because I normally do other stuff, and I just got in the habit).

But, as is the case between PHP3 and forward (PHP4 and 5 (thanks for the info ;-) ) ), you never know when the inheritance functionality will change, and that would definitely be a hard bug to track down.




Theme © iAndrew 2016 - Forum software by © MyBB