Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

init, _init, _construct, __construct
 
apl
Member
 
Total Posts:  33
Joined:  2008-12-16
 

As far as I know, __construct comes with PHP, then Zend Framework invents init. Now I see _init and _construct everywhere in Magento.

What are the differences between them, particularly _init vs _construct? I need to know which constructor is available without browsing up all parent classes.

Thanks very much

 
Magento Community Magento Community
Magento Community
Magento Community
 
anutosh
Member
 
Total Posts:  46
Joined:  2010-03-05
Kolkata, India
 

Well I didn’t know about “_init” & “init” methods. But the two types of construct methods have been disturbing me ever since I started off with Magento. I would also like to know about the detail differences between them.

Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

I may be wrong, but here is what I understood by looking through the code.
Almost all classes in Magento inherit Varien_Object.
This class implements __construct method and inside it calls _construct.
The recommendation is to override _construct instead of __construct, so there will be a common access point for all the classes.
Let’s say that in the future Varien decides to add an action each time a class is initialized. They will add it in __construct method of the Varien_Object class and it will get called in all the classes that extend this.

Now for the _init method.
From my point of view Magento is not exactly a MVC application.
It has an other layer between models (M) and the database. This layer is the ‘Resource Model’.
This layer does the actual reading and writing form/into the database. and the models use the data retrieved by the resource models and send data to them for writing in the DB.

In order to see _init works see Mage_Core_Model_Abstract class but it won’t tell you much.

I hope I cleared a few things, or at least I didn’t confuse you more.
A word from a core team member would be nice.

 
Magento Community Magento Community
Magento Community
Magento Community
 
apl
Member
 
Total Posts:  33
Joined:  2008-12-16
 

thanks, tzyganu.

a ten or twenty minutes systematic tutorial from authoritative source would be very ncie.

 
Magento Community Magento Community
Magento Community
Magento Community
 
anutosh
Member
 
Total Posts:  46
Joined:  2010-03-05
Kolkata, India
 

@tzyganu - Thanks very much for this much info, at least. And really, a word from the Core team s really needed for knowing the importance of these 4 methods:-
1. _construct
2. __construct
3. init
4. _init

tzyganu - 25 November 2010 03:37 AM

I hope I cleared a few things, or at least I didn’t confuse you more.
A word from a core team member would be nice.

And please don’t worry about confusing, as you have some good knowledge about Magento, which will not at all confuse many newbies.

Some of us will really wait for details.

 
Magento Community Magento Community
Magento Community
Magento Community
 
anutosh
Member
 
Total Posts:  46
Joined:  2010-03-05
Kolkata, India
 

@tzyganu - After all these months, I think that may be I can understand your words now:-

tzyganu - 25 November 2010 03:37 AM

Almost all classes in Magento inherit Varien_Object.
This class implements __construct method and inside it calls _construct.
The recommendation is to override _construct instead of __construct, so there will be a common access point for all the classes.
Let’s say that in the future Varien decides to add an action each time a class is initialized. They will add it in __construct method of the Varien_Object class and it will get called in all the classes that extend this.

Thanks a lot to ”primatology” also, for his detailed post in here.

class BaseClass {
   
public function __construct() {
       
print "Base Class constructor\n";
       
anotherMethod();
   
}

   
public function anotherMethod() {
      
print "Another Method\n";
   
}
}

class SubClass extends BaseClass {
   
function __construct() {
       
print "Sub Class constructor\n";
       
/**
        * Here the parent Constructor does not get called, as the object has already been initialized by this constructor at least.
        * If you want the base class constructor to get called, then you will have to specifically mention this statement:-
        * parent::__construct();
        * But unfortunately, it\'s not what we developers always do, and also the Magento Team does not expect us to do also;
        * so they have designed this extra method "_construct()", for the developers to override, as correctly mentioned by "[b]tzyganu[/b]".
        */
   
}
}

$obj 
= new BaseClass();
Output:-
Base Class constructor
Another Method

$obj 
= new SubClass();
Output:-
Sub Class constructor

So the correct functionality should be:-

class BaseClass {
   
public function __construct() {
       anotherMethod
();
       
_construct();
   
}

   
public function anotherMethod() {
      
print "Another Method\n";
   
}

   
public function _construct() {
       
print "Base Class constructor\n";
   
}
}

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

$obj 
= new BaseClass();
Output:-
Another Method
Base 
Class constructor

$obj 
= new SubClass();
Output:-
Another Method
Sub 
Class constructor

Thanks to both ”tzyganu” and ”primatology”, for detailing out this feature of Magento.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top