Well, when you instantiate Class4 PHP will check for it's constructor. If it has one, it will call it. If not it will check Class3, then Class2, then Class1.

Once it finds a constructor, it stops looking for more, but if you want it to resume checking the the parents for constructors you must call parent::_construct() from the one it found.

If you define a constructor in Class4, It will run in all but one circumstance. If a child has a constructor, but does not call parent::_construct() from it. That would stop this waterfall effect.


FrzzMan wrote:

Yeah you understood me, thanks god :D but not fully :(

But the problem is right in your code (sorry :D)

What if I create an instance of ChildClass, its constructor won't be called (since it commented out), but if I un-comment its constructor, it will be called even if I create an instance of GrandChildClass?

Let me make some simple diagram ;)

Class1 <-- Class2 <-- Class3 <-- Class4

If I create an instance of Class4, then Class4's constructor and Class1's constructor must be called, neither Class3 nor Class2.

You can see that Class1's constructor will always be called, and the constructor of the lowest class in the class tree will be called.

I think this is a bit complex, I can define a final function in Class1, and call it in every deriver class constructor, it will solve the problem (I think) but it's not convenience, I want it done automatically...

Any idea are welcome :D

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to