The rewrite mechanism of magento and the hidden characteristics of OOP

Source: Internet
Author: User

In object-oriented programming, we all know that the class has overloaded (overload), overlay (override), Hide (hide) Three relationships, I believe many people have been very understanding of them, so the focus of today's analysis is not these, but the rewrite mechanism in Magento, Why would you mention the three features of OOP first, because I think they are similar to the rewrite mechanism of Magento, but the similarity is not the same, or first look at the code, it is easier to understand.

Magento Send mail, use the built-in core/email_template, now rewrite this model send function, add the following in Config.xml:[PHP]View plain copy <models> <core> <rewrite><email_template>company_mymodel_model_ema il_template</email_template> </rewrite> </core> </models>
Then company/mymodel/model/email/template.php add the following code:[PHP]View plain Copy class Company_mymodel_model_email_templateextends mage_core_model_email_template {public function Send ($email, $name =null, array $variables = Array ()) {//omitted, emphasis not on how to rewrite send}

You can write this when you send a message:

$mailConfirm = Mage::getmodel (' core/email_template ');

$mailConfirm->send (//parameter ...);

Or

$mailConfirm = Mage::getmodel (mymodel/email_template ');

$mailConfirm->send (//parameter ...);

Notice the difference in the call (red font), one is still called the built-in model, one is the rewrite model, you can guess what the result is. Yes, the result is always the Send function that calls Company_mymodel_model_email_template. Is it a bit like the hidden features mentioned in the beginning, because the feeling is that Company_mymodel_model_email_template sent to the core of the send to hide, I did not say is and cover similar, my God, Forget it or paste the three characteristics of the definition, to understand the following:

/////////////////////////////////////////////////////////////////////////////

overloading means that different functions use the same function name, but the number or type of arguments for a function is different. When invoked, different functions are distinguished by the parameters of the function.

Features that member functions are overloaded
(1) The same range (in the same class);
(2) The function name is the same;
(3) different parameters;
(4) virtual keyword is optional.

Overriding (also called rewriting) refers to a derived class function that overrides a base class function to recreate a virtual function (note is a virtual function) in the base class in a derived class. The function names and parameters are the same, but the functions of the implementation of the body is not the same.

Feature is
(1) different ranges (in derived classes and base classes, respectively);
(2) The function name is the same;
(3) the same parameter;
(4) The base class function must have the virtual keyword.

"Hide" means that a function of a derived class masks a function in a derived class that has the same name as the one in the base class, which masks the functions of the same names in the underlying classes. Hiding is similar to the other two concepts on the surface, it is difficult to distinguish, in fact, their key difference is in the implementation of polymorphism. What is polymorphic. To put it simply is an interface, a variety of implementations.

The rules are as follows
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that you are not confused with overloading).
(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (note that it is confused with overrides)
3 kinds of conditions how to carry out: 1. Overload: See Parameter 2. Hide: Call what on 3. Overriding: Invoking derived classes

///////////////////////////////////////////////////////////////////////////////

Because the two send name parameter return values are the same, and the base class send has no virtual qualification, I say it is similar to hiding. Why is it just like that? When called, for hidden attributes, what to call what, such as the parent class, the new parent class object, with subclasses, the new subclass. But the rewrite of Magento is not, for example, the two methods of calling Send on the top, one is Getmodel's core (that is, the parent), and the other is the Getmodel inheritance class, which should logically call different send, But the result invocation is always the rewritten send.

By tracking the Magento source discovery, in the Mage_core_model_config Getgroupedclassname method:

$config = $this->_xml->global->{$groupType. ' s '}->{$group};

if (Isset ($config->rewrite-> $class)) {

$className = (string) $config->rewrite-> $class;

} else {

。。。。。。

}

At the time of Mage::getmodel (' Core/email_template '), Magento first made a judgment, because Coremodel Email_template had been rewrite in the front config, So although the call is the core send, the execution is still rewrite send, if it is hidden, call the core send, the execution should be the core send; so if you have ever rewrite the core email_template, Or there are other Third-party components rewrite (of course you may not know or forget), debugging, in the core of the email_template to change, is not responsive, and very puzzled, according to the hidden rules of OOP, I also use the Mage:: Getmodel (' core/email_template ') to invoke, the new core/email_template, how in core/email_template modification is not responding to it. Because I've had this problem before. It is not known that there is no adherence to the hidden principles of OOP, because has been magento rewrite mechanism to get rid of.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.