Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to extend classes further?

I am wondering is is possible and how would it look if I extended class further then to one child. And when I do how would I access the main parent methods and propertys?

This is not my code, but that is the idea I am trying to understand.
`

class A{
    $n;
 }
 class B extends A {
    //Do somthing
 }
 
 class C extends B{
    $this->n = "Hello"
 }

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And also does A constructor run if I init C?
And at the top of php file I have to include both files where the classes are located, correct?

>Solution :

Yes, it is possible to extend a class further down the inheritance chain, as you have shown with the classes B and C extending A. When you extend a class, the child class will have access to all of the methods and properties of the parent class, as well as any methods and properties that are defined in the child class itself.

In the example you provided, the value of $n would be set to "Hello" in the C class. If you instantiate an object of the C class, you would be able to access the value of $n by calling $object->n

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading