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 inherits class 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); } }.

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 inherits class 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); } }.

Just about to post the same code. +1 – MitMaro Oct 13 '09 at 0:30.

I ended up coming up with an alternative solution that solved the problem. I created an intermediate class that extended Grandpa. Then both Papa and Kiddo extended that class.

Kiddo required some intermediate functionality of Papa but didn't like it's constructor so the class has that additional functionality and both extend it. I've upvoted the other two answers that provided valid yet ugly solutions for an uglier question:).

2 I think the better idea here is to break the functionality you are trying to use out of the constructed and into a protected method. Then you can call that method from a constructor selectively – xximjasonxx Jun 14 '10 at 2:56.

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.

This won't work afaik because __construct can't be static – Mark Oct 13 '09 at 0:21 No, it works as expected. – too much php Oct 13 '09 at 0:54 Can't understand how. Declaring __construct as static results in the following error for me "Fatal error: Constructor Grandpa::__construct() cannot be static" under PHP5.3 – Mark Oct 13 '09 at 4:08 When I tried it, I didn't declare it as static.

I created the class structure normally but instead of calling parent::__construct(), I called Grandpa::__construct() and it worked. I doesn't seem right to me either but this whole question got kinda weird. – Paulo Oct 13 '09 at 4:25.

Another option that doesn't use a flag and might work in your situation: GrandpaSetup(); } public function GrandpaSetup(){ $this->prop1 = 'foo'; $this->prop2 = 'bar'; } } class Papa extends Grandpa { public function __construct() { // call Grandpa's constructor parent::__construct(); $this->prop1 = 'foobar'; } } class Kiddo extends Papa { public function __construct() { $this->GrandpaSetup(); } } $kid = new Kiddo(); echo "{$kid->prop1}\n{$kid->prop2}\n.

Ok, Yet another ugly solution: Create a function in Papa like: protected function call2Granpa() { return parent::__constructor(); } Then in Kiddo you use: parent::call2Granpa(); //instead of calling constructor in Papa. I think it could work... I haven't test it, so I'm not sure if the objects are created correctly. I used this approach but with non-constructor functions.

There's an easier solution for this, but it requires that you know exactly how much inheritance your current class has gone through. Fortunately, get_parent_class()'s arguments allow your class array member to be the class name as a string as well as an instance itself. Bear in mind that this also inherently relies on calling a class' __construct() method statically, though within the instanced scope of an inheriting object the difference in this particular case is negligible (ah, PHP).

Consider the following: class Foo { var $f = 'bad (Foo)'; function __construct() { $this->f = 'Good! '; } } class Bar extends Foo { var $f = 'bad (Bar)'; } class FooBar extends Bar { var $f = 'bad (FooBar)'; function __construct() { # FooBar constructor logic here call_user_func(array(get_parent_class(get_parent_class($this)), '__construct')); } } $foo = new FooBar(); echo $foo->f; #=> 'Good! ' Again, this isn't a viable solution for a situation where you have no idea how much inheritance has taken place, due to the limitations of debug_backtrace(), but in controlled circumstances, it works as intended.

I need to have a class constructor in PHP call its parent's parent's (grandparent?) constructor without calling the parent constructor. I know this is a bizarre thing to do and I'm attempting to find a means that doesn't smell bad but nonetheless, I'm curious if it's possible. I thought I should post the rationale for the chosen answer.

The reason being; it most elegant solutionto the problem of wanting to call the "grandparent's" constructor while retaining all the values. It's certainly not the best approach nor is it OOP friendly, but that's not what the question was asking. For anyone coming across this question at a later date - Please find another solution.

I was able to find a much better approach that didn't wreak havoc on the class structure. So should you.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions