Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Magento have _construct and __construct methods?

Is there a reason why Magento has a _construct and a __construct method? Why does the additional _construct exist? Could anything achieved by having the extra _construct method not be achieved by just calling the parent constructor in the child class?

like image 407
Nick Avatar asked Jan 02 '12 23:01

Nick


People also ask

What is the use of __ construct?

PHP - The __construct Function A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

What does __ construct mean?

__construct() is the method name for the constructor. The constructor is called on an object after it has been created, and is a good place to put initialisation code, etc.


2 Answers

Best answer I can find: http://www.magentocommerce.com/boards/viewthread/76027/#t282659

Basically, the root-level class (from which all other classes inherit) implements __construct, which PHP calls automatically whenever a class is constructed. Right now, this root-level class simply calls _construct, which contains the actual code.

Say you have this set-up:

class BaseClass {    function __construct() {        print "In BaseClass constructor\n";        doSomethingReallyImportant();    } }  class SubClass extends BaseClass {    function __construct() {        print "In SubClass constructor\n";    } }  $obj = new BaseClass(); //"In BaseClass constructor" //something really important happens  $obj = new SubClass(); //"In SubClass constructor" //important thing DOESN'T happen 

PHP doesn't automatically call the parent class constructors, so doSomethingReallyImportant never gets called. You could require that subclass constructors call parent::__construct(), but that's easy to forget. So Magento has subclasses override _construct:

class BaseClass {    function __construct() {        doSomethingReallyImportant();       _construct();    }    function _construct() {        print "In BaseClass constructor\n";    } }  class SubClass extends BaseClass {    function _construct() {        print "In SubClass constructor\n";    } }  $obj = new BaseClass(); //something really important happens //"In BaseClass constructor"  $obj = new SubClass(); //something really important happens //"In SubClass constructor" 

PHP doesn't detect a constructor in SubClass, so it calls BaseClass's constructor. This allows BaseClass to doSomethingReallyImportant before calling SubClass's overridden _construct.

like image 105
benesch Avatar answered Oct 05 '22 11:10

benesch


To Marco: it is wrong to override __construct() method like this in Magento. The reason is - all classes inherit it from Varien_Object and it has this code:

#File: lib/Varien/Object.php  public function __construct()  {            //...snip...                    $args = func_get_args();            if (empty($args[0]))        {                    $args[0] = array();            }            //...snip...  }  //...  

With the __construct using your code, those arguments don’t get passed through. You really have to use Benesch's code:

class SubClass extends BaseClass {    function _construct() {        print "In SubClass constructor\n";    } } 

Read more about this in Magento Block Lifecycle Methods by Alan Storm

like image 25
PICher Avatar answered Oct 05 '22 11:10

PICher