Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using constructors is sort of a bad example of how this anti-pattern emerges, since in PHP like in most languages that support inheritance,</p> <blockquote> <p>If the child does not define a constructor then it may be inherited from the parent class just like a normal class method ... (from the <a href="http://php.net/manual/en/language.oop5.decon.php" rel="nofollow">PHP manual</a>).</p> </blockquote> <p>If in any of the other 998 classes you are trying to define you purposely do not include a constructor, the base constructor will get called, making it pretty much a requirement to <em>add a constructor on each derived class</em> to avoid this functionality. This is why is not recommend to add logic in a constructor that might alter the state of protected fields/properties or to call virtual methods that might be overridden in derived classes.</p> <p>However, the anti-pattern actually expresses that:</p> <blockquote> <p>Note that it is <em>the requirement of calling the parent</em> that is the anti-pattern (from <a href="http://en.wikipedia.org/wiki/Call_super" rel="nofollow">Wikipedia</a>)</p> </blockquote> <p>So, <em>it is OK to call the base class version of a method as long as it is <strong>not</strong> required to do so by a derived class</em>. The derived class must behave correctly even though the base method is never called. This will avoid the scenario where a derived class overrides a virtual method but does not implement some special requirement (perhaps because of lack of documentation on the base class), at which point the derived class does not behaves as expected by the system, potentially causing the system to crash or behave unexpectedly, a typical violation of the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle" rel="nofollow">Liskov substitution principle</a>.</p> <p>In your particular example this anti-patter does not apply: you're extending constructors, which by definition are required to be called as they instantiate the class into a useful state. And in most instances that's what you want.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload