Design Templates - Challenge vs. Super Template - Avoid or Save

I recently noticed that something that I really liked doing in some cases (call super) is an anti-pattern. ( http://en.wikipedia.org/wiki/Call_super )

So my question is:

How to do the following (the main thing that most of us require is to add some details to the object). I will just add a new element to the array that is held in the var object:

<?php class A { // bla bla bla public function __construct() { $this->_data['newDetail'] = "Ipiicaei"; } } class B extends A { // bla bla bla // Override constructor, I need to add one more detail for this class public function __construct() { parent::__construct(); $this->_data['newDetailSubcls'] = "Something"; } } 

Now ... if I do not call the constructor of the parent class to add the first element, I have 2 possibilities:

  • In each subclass, add the part of the code that the parent usually executes and remove it from the parent. Therefore, if I extend it to 999 classes, I will duplicate 999 * lines in the parent. Sounds bad to me.

  • Call the method from the parent, which is implemented in the child (the template method template, which he recommends). Therefore, if I need only one class to add this, and the rest of 998 should behave exactly like the parent, in each of them I add an empty function. It also sounds bad to me.

Keep in mind that my example is simple (what the example should look like), but the parent class and / or subclass can do complex things.

I see the reasons why a super call can be bad in some situations. But this seems good to me.

So ... how would you approach? Ignore that calling super is an anti-pattern and do what I do (or used to do if it turns out that my path is bad)? Or how?

+4
source share
1 answer

Using constructors is an example of a bad example of how this anti-pattern arises, since in PHP, as in most languages ​​that support inheritance,

If the child does not define a constructor, it can be inherited from the parent class in the same way as the method of the normal class ... (from the PHP manual ).

If in any of the other 998 classes you are trying to determine that you are not intentionally including a constructor, the base constructor will be called, which makes it pretty necessary to add a constructor to each derived class to avoid this functionality. This is why it is not recommended to add logic to the constructor, which can change the state of protected fields / properties or call virtual methods that can be overridden in derived classes.

However, the anti-pattern actually expresses that:

Please note that this is a requirement to call the parent, which is an anti-pattern (from Wikipedia )

So, it’s ok to call the version of the base class of the method if it is not required for the derived class. A derived class must behave correctly, even if the base method is never called. This will avoid a scenario in which the derived class overrides the virtual method but does not implement some special requirements (possibly due to the lack of documentation in the base class), and at this point the derived class does not behave as expected by the system, which may cause the system for an unexpected failure or typical violation of a typical violation of the Liskov replacement principle .

In your specific example, this antipater is not used: you extend the constructors, which by definition should be called, because they create the class in a useful state. And in most cases, this is what you want.

+4
source

Source: https://habr.com/ru/post/1486640/


All Articles