PHP class inheritance depth

放肆的年华 提交于 2019-12-02 09:25:06

Sounds like a problem with your editor. For example, see the following quick-and-dirty demonstration:

php > class a { function foo() { echo "A::foo()\n"; } }
php > $letters = range('a', 'z');
php > for ($i = 0; $i < 25; $i++) { eval("class {$letters[$i+1]} extends {$letters[$i]} {}"); }
php > $z = new z;
php > $z->foo();
A::foo()

PHP doesn't impose any kind of restriction like this on you.

Using a canonical test like this:

<?php
abstract class A {
    abstract function getValue();
}
abstract class B extends A{ }
abstract class C extends B{ }
abstract class D extends C{ }
abstract class E extends D{ }
abstract class F extends E{ }
class G extends F{

}
?>

one would expect a fatal error in that G does not in fact implement the abstract method defined in A. As you can see from the link above, this is in fact the case.

Thus, while this is a deep inheritance, it is beneath whatever limit (if any) PHP has. Rather, you're likely running into an indexer issue in the PDT.

The following php:

class A
{
    public function FooA()
    {
        echo "A!!";
    }
}

class B extends A
{
    public function FooB()
    {
        echo "B!!";
    }
}

class C extends B
{
    public function FooC()
    {
        parent::FooB();
        parent::FooA();
    }
}

$test = new C();
$test->FooC();

prints:

B!!A!!

I tested this at a depth of 50 and it still functioned just fine, so you definitely can, sounds like your editor plug-in only looks so deep in the inheritance tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!