• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

PHP constructor execution order

In C++, when there is an inheritance hierarchy, constructors are executed in a special order (eg, Base Class, than Derived). Does PHP have anything like this?
0
msosno
Asked:
msosno
  • 2
2 Solutions
 
fcardinauxCommented:
You need to call the parent constructor in the derived class constructor, like in the following code.

This determines the order in which constructors are called.
class myDerived extends myParent 
{
    function __construct()
    {
        // some instructions ...
        parent::__construct();
        // some instructions ...
    }
}

Open in new window

0
 
Ray PaseurCommented:
Some required reading here (depending on whether you are PHP4 or PHP5)

PHP4
http://us3.php.net/manual/en/language.oop.php

PHP5 (hopefully)
http://us3.php.net/manual/en/language.oop5.php

Best regards, ~Ray
0
 
fcardinauxCommented:
You're right, Ray. I forgot to mention that my example was in PHP5: http://us3.php.net/manual/en/language.oop5.decon.php

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now