Home > Blockchain >  PHP Classes Can i use B extend A class in C class?
PHP Classes Can i use B extend A class in C class?

Time:10-20

Please check the example can I use the A function in C like the below example? If not then how can I use the A function in C?

class A {
    public function a()
    {
       return 'hello';
    }
}
class B extends A {
    //
}
class C extends B {
    // Can I use the A function in C?
}

CodePudding user response:

Methods are inherited through any number of nested classes.

class A {
    public function a()
    {
        return 'hello';
    }
}
class B extends A {
    //
}
class C extends B {
    function c() {
        echo $this->a();
    }
}

$x = new C();
$x->c(); // This will print `Hello`.
echo $x->a(); // So will this.
  • Related