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.
source share