How do I get a PHP class constructor to call its parent's parent's constructor? How do I get a PHP class constructor to call its parent's parent's constructor? php php

How do I get a PHP class constructor to call its parent's parent's constructor?


The ugly workaround would be to pass a boolean param to Papa indicating that you do not wish to parse the code contained in it's constructor. i.e:

// main class that everything inheritsclass Grandpa {    public function __construct()    {    }}class Papa extends Grandpa{    public function __construct($bypass = false)    {        // only perform actions inside if not bypassing        if (!$bypass) {        }        // call Grandpa's constructor        parent::__construct();    }}class Kiddo extends Papa{    public function __construct()    {        $bypassPapa = true;        parent::__construct($bypassPapa);    }}


You must use Grandpa::__construct(), there's no other shortcut for it. Also, this ruins the encapsulation of the Papa class - when reading or working on Papa, it should be safe to assume that the __construct() method will be called during construction, but the Kiddo class does not do this.


class Grandpa {    public function __construct()    {}}class Papa extends Grandpa{    public function __construct()    {        //call Grandpa's constructor        parent::__construct();    }}class Kiddo extends Papa{    public function __construct()    {        //this is not a bug, it works that way in php        Grandpa::__construct();    }}