我需要在PHP中有一个类构造函数调用父类的父类的(祖父母?)构造函数,而不调用父类构造函数。

// main class that everything inherits
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 WHERE I NEED TO CALL GRANDPA'S
        // CONSTRUCTOR AND NOT PAPA'S
    }
}

我知道这是一件很奇怪的事情,我正试图找到一种不难闻的方法,但尽管如此,我很好奇这是否可能。


当前回答

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();
    }
}

其他回答

你必须使用外公::__construct(),没有其他的快捷方式。此外,这也破坏了Papa类的封装——当读取或处理Papa时,应该可以安全地假设__construct()方法将在构造过程中被调用,但Kiddo类不会这样做。

从PHP 7你可以使用

家长:家长:__construct ();

我最终想出了一个解决问题的替代方案。

我创建了一个中间类,扩展了爷爷。 然后爸爸和孩子都延长了这个课程。 Kiddo需要Papa的一些中间功能,但不喜欢它的构造函数,所以类有额外的功能,并扩展了它。

我对另外两个答案大加赞赏,它们为一个更丑陋的问题提供了有效但丑陋的解决方案:)

另一个不使用标志的选项可能适用于您的情况:

<?php
// main class that everything inherits
class Grandpa 
{
    public function __construct(){
        $this->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";

对此有一个更简单的解决方案,但它要求您确切地知道当前类已经经历了多少继承。幸运的是,get_parent_class()的参数允许类数组成员作为一个字符串作为类名,也可以作为实例本身。

请记住,这本质上也依赖于静态调用类的__construct()方法,尽管在继承对象的实例化范围内,这种特殊情况下的差异可以忽略不计(啊,PHP)。

考虑以下几点:

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!'

同样,由于debug_backtrace()的限制,对于不知道发生了多少继承的情况,这不是一个可行的解决方案,但在受控的情况下,它可以按预期工作。