What is inheritance in PHP?
Answer
Inheritance in PHP allows a class to inherit properties and methods from a parent class using the extends keyword: class Dog extends Animal { }. The child class gets all public and protected members of the parent and can override them. PHP supports only single inheritance — a class can extend only one parent class. Use parent:: to call the parent class method or constructor: parent::__construct($name);. You can prevent a class from being extended by declaring it final. You can also prevent a specific method from being overridden by declaring it final. Traits compensate for the single-inheritance limitation by enabling horizontal code reuse.
Previous
What is the difference between public, protected, and private in PHP?
Next
What is an interface in PHP?