PHP defaults to the parent constructor if your class doesn't have one. So why do i see people commonly do something like this:

class parentClass {
  public function __construct()
   {
      echo 'working';
   }
}

class childClass extends parentClass {
   public function __construct()
   {
       return parent::__construct();
   }
}

What is the advantage to returning the parent construct if it behaves the same without it? Perhaps it doesn't behave the same?

Thank you for any help. Just trying to better understand the language.

    this is direct from the the php manual "when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality. " a __constructor is just a method. On a more detailed note that php will call the closest constructor if a class constructor is not found on inherited classes. i suggest you thoroughly read through these things and then re-read'em.... you will get it.

      Perhaps you didn't read my question.

      What is the advantage to returning the parent construct if it behaves the same without it?

      You sort of just told me what I already said. This was a best practices question, not a functionality question.

      So why do many frameworks use construct { return parent::construct(); } simply to prevent overwriting the construct, or does it have something to do with something else (perhaps late static binding).

        this is a complicated process to explain... such frameworks have you extend aka the controller for mainly 2 reasons to pass the registry object or loader object.... so you can load from the library easier. second it keeps from having to initiate the same class twice by the system and by the application helps with identifying seperation from core elements and and application elements.

          Write a Reply...