开发者

PHP class inheritance depth

开发者 https://www.devze.com 2023-02-07 02:55 出处:网络
I\'m trying to work out exactly how deep inheritance goes. For example if I start with class A Class B extends class A

I'm trying to work out exactly how deep inheritance goes.

For example if I start with class A

Class B extends class A

Class C extends class B

Class D extends class C

Class E extends class D

Class F extends class E

Class G extends class F

most of the lower levels are abstract classes with methods filled with code and abstract methods. its a rather large and complex structure. (doing my head in)

Will Class G still be able to access Class A methods and parameters, as well as still have access to the abstract methods from Class A ?

I'm asking because I have been having trouble with eclipse not adding the lower class methods into the auto complete. I would like to know if there is actually a limit to this or whether it's just eclipse reaching its maximum for code completion.

I'm far from a stage where I can test this with what I have done 开发者_StackOverflow社区and don't want to find when I'm done that methods defined lower in the order are not accessible.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消