Home > Article > Backend Development > Overriding in PHP
Overriding is an Object-Oriented Programming concept that is similar to a concept like Class, Object, Encapsulation, Polymorphism, Overloading etc in PHP. Overriding of functions and classes are done when a method in the derived class is created which is the same as that of the method in the base class or parent class. Both of these methods have the same name and same number of arguments.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Let’s explore how overriding works in PHP.
The example of method overriding is written below.
Code:
class BaseClass { public function ABC() { echo "<br /> In the base class"; } } class DerivedClass extends BaseClass { // override the method ABC() of base class public function ABC() { echo "<br />In the derived class"; } } $obj1 = new BaseClass; $obj1->ABC(); $obj2 = new DerivedClass; $obj2->ABC();
Output:
There are three access modifiers.
As we know the protected method is accessible from a base class and the derived class it can be made public in the subclass but not private as private is only accessible in the parent class. Also if a class method has an access specifier as public then the overriding method in the derived class cannot be declared as private and protected
An example of overriding using access modifiers is written below.
Code:
class BaseClass { private function ABC() { echo "<br/>In the base class Method : ABC"; } protected function XYZ() { echo "<br/>In the base class Method : XYZ"; } } class DerivedClass extends BaseClass { // overriding with public for wider accessibility public function ABC() { echo "<br/> In the derived class Method : ABC"; } // overriding method // with more accessibility public function XYZ() { echo "<br/>In the derived class Method : XYZ"; } } //$obj1 = new BaseClass; //$obj1->ABC(); //throws fatal error //$obj1->XYZ(); //throws fatal error $obj2 = new DerivedClass; $obj2->ABC(); $obj2->XYZ();
Output:
The final keyword is used for classes and methods. Methods and Classes, not variables can be overridden.
When a method or a class is declared as final then overriding on that method or class cannot be performed also inheritance with the class is not possible.
The example of overriding using final keyword is written below.
Code:
class BaseClass { // Final method – display // this cannot be overridden in base class final function display() { echo "<br /> In the Base class display function"; } /// method - ABC function ABC() { echo "<br /> In the Base cLass ABC function"; } } class DerivedClass extends BaseClass { function ABC() { echo "<br /> In the Derived class ABC function"; } } $obj1 = new DerivedClass; $obj1->display(); $obj1->ABC();
Output:
A class declared as final cannot be inherited. A Final Class further have final method along with other methods. But since the class itself is declared final there is no use of declaring a final method in a final class.
The example of class overriding using final keyword is written below.
Code:
// class declared as final cannot be overridden final class BaseClass { // method - ABC function ABC() { echo "<br> In the BaseClass Method ABC function"; } // Final method - display function display() { echo "<br> In the BaseClass Method display function"; } } // here you cannot extend the base class // as the base class is declared as final $obj1 = new BaseClass; $obj1->display(); $obj1->ABC();
Output:
The above is the detailed content of Overriding in PHP. For more information, please follow other related articles on the PHP Chinese website!