Top Ad unit 728 × 90

Welcome !!! To The World of Programming

OOPS FEATURES

Oops

PHP - Method Overriding

 

Method Overriding in PHP


PHP in web development is often associated with "Personal Home Page," as it was originally created by Rasmus Lerdorf in 1994 as a tool for managing his personal website.

PHP stands for "Hypertext Preprocessor." It is a widely used server-side scripting language designed for web development. PHP scripts are executed on the server, and the resulting HTML is sent to the client's web browser, making it a vital technology for building dynamic and interactive websites and web applications.

method-overriding-in-php


Method Overriding
Class Diagram
Method overriding in PHP allows a subclass (a class that extends another class) to provide a specific implementation for a method that is already defined in its parent (or superclass) class.

This allows the developer to customize the behavior of a method in the subclass and must preserve the method's name and signature from the parent class.

The class diagram shown above is implemented in PHP. The Person class is defined as follows:



<?php 
class Person
{
    //fields to store First and Last Name of a person
	//Declares as protected to make it accessible in Employeee class
	protected $firstName;
	protected $lastName;
        //Constructor to initialize fields
	public function __construct($firstName,$lastName)
	{
		$this->firstName = $firstName;
		$this->lastName = $lastName;
	}
        //Displays name in Firstname and Lastname format
	//Can be overridden in sub class
	public function completeName()
	{
		echo $this->firstName." ".$this->lastName;
	}
}
?>

The Employee class is defined by inheriting from the Person class. Python provides extends keyword  similar to Java to inherit a class. The constructor is also defined to pass parameters required by Person class constructor. PHP provides parent keyword to pass parameters from Employee class(Sub class) to the Person class(Super class) from the constructor of the sub class.

parent:: Keyword: Inside the overridden method, One can use the parent:: keyword to call the parent class's implementation of the method if needed.

The completeName() method gets inherited from the Person class. It displays the name of the Person in the First and Last name format. 
Method Overriding is required when name of the Employee is required in the Last and First name format. It is implemented by redefining  completeName() method in the Employee class with the same signature as demonstrated in the code below:

<?php 
class Employee extends Person 
{
  //It is compulsory to define constructor in Sub class
  //As no default constructor in super class
  //Its job is provide parameters required by super class constructor
  public function __construct($firstName, $lastName)
  {
     parent::__construct($firstName,$lastName);
  }
  //Method inherited from Person is overridden here
  //Displays name in Lastname and Firstname format
  public function completeName()
  {
    echo $this->lastName." ".$this->firstName;
  }

}

?>


The Testing code is as follows:-

<?php
$person = new Person("Neeraj","Chopra");
$person->completeName();
$person = new Employee("Neeraj","Chopra");
$person->completeName();
?>

The output of the above code in the Browser is:


Wondering:

  • Why all that lengthy code?
  • Why not make change in Person class code to get the work done?
Remember in a typical development Scenario, a team of developers working who may be from different organizations. One developer has developed the Person class and shared .jar file with other developers. Source code is not shared therefore code is not available to update it. 
What if both the versions of the completeName() is required?

Method Overriding is feature of Object-Oriented Languages to achieve runtime polymorphism. Recall in When Person(Super class) Object refers to the object of the Person class calls the super class version of the completeName() method and when it refers to the object of the Employee class (Sub class) calls the sub class version of the completeName() method.

However, the developed of Person may decide not to allows the users of his/her class to override than the developer can mark completeName() method with the final keyword.

public final function completeName() 
{
   
}

Note:

PHP is a Server-Side Scripting language used to create a dynamic and an interactive Web Sites. Web Applications are based on Client-Server model. Software required on Server is termed as a Web Server and at Client a browser is used to surf the Web Page.
For PHP,  XAMPP or WAMP Servers are required to deploy the Web site. A Web Site is a collection of Web Pages. It is then browsed using a browser like Chrome, Internet Explorer or Mozilla Firefox. 


PHP - Method Overriding Reviewed by Syed Hafiz Choudhary on September 17, 2023 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.