Home  >  Article  >  Backend Development  >  Overriding in PHP

Overriding in PHP

WBOY
WBOYOriginal
2024-08-29 12:59:101029browse

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How does Overriding Work?

Let’s explore how overriding works in PHP.

  • Overriding is a concept where the derived class of the base class performs the same action as that of a base class. This overriding can be on methods or classes. If it is method overriding then the name of the method the parameters, the access specifier is found to be the same as that of the parent class method. Where there is no change found in the implementation of the method in the subclass derived from the base class, at that time it is said that the method of the subclass has overridden the method of the base class.
  • Suppose there is a class called Person which has its own data properties and methods. This is the parent or base class Person with a method called speak(). Now there is one more class called Employee which extends the Person class. Now, this Employee class has its own data properties and methods and also has a method same as that of the parent class eg.speak().
  • This, speaks () method is present in the base class and also in the derived class.
  • Now what happens is when the class is instantiated or when the object is created, which of the speak() method will be executed, meaning that speak of the base class or speak of the derived class is executed is dependent on the object of the class is called.
  • In other words, if an object of Person class is created then the speak() method of the Person class is called and is therefore executed. But if the object of the Employee class is created then the speak() method of the derived class will be executed, which overrides the speak() class of the parent class.
  • And here is how overriding takes place with an example
  • In the below example, there are two classes, base class, and derived class. The derived class extends the base class. These classes are initiated and two objects $obj1 and $obj2 are created. $obj1 is an object of BaseClass and $obj2 is an object of a derived class. These objects further call the methods declared in their respective classes.
  • Here you will observe that the base class and the derived class have the same method called ABC(). When you execute this program, you will notice that the ABC() method has overridden the base class method ABC().

Example of Method Overriding

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:

Overriding in PHP

Overriding with Access Modifiers

There are three access modifiers.

  1. public: Accessible from anywhere in the program.
  2. private: Accessible from the parent class only.
  3. protected: Accessible from the base class and the derived class.

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

Example of Method Overriding using Access Modifiers

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:

Overriding in PHP

Overriding with Final Keyword

The final keyword is used for classes and methods. Methods and Classes, not variables can be overridden.

Final Method Overriding

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.

Example of Method Overriding using Final Keyword

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:

Overriding in PHP

Final Class Overriding

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.

Example of Class Overriding using Final Keyword

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:

Overriding in PHP

The above is the detailed content of Overriding in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP XORNext article:PHP XOR