Inheritance in PHP – Linux Hint

The three main characteristics of object-oriented programming are encapsulation, inheritance and polymorphism. When programming, it may be necessary to re-use a particular piece of code several times, while the use of inheritance reduces the need to rewrite code manually by reusing it. Inheritance is a method of creating a new class by inheriting a base class. The purpose of the new class will be to have access to all members of the new class and the base class through inheritance. The same code can therefore be used several times by writing it only once. PHP uses the keyword extension for inheritance. This tutorial shows you how to implement legacy with a PHP script.

Successor Syntax

The syntax of the follow-up is presented below.

The new class expands the old class
{

}

Here the newClass subclass, derivative or subclass is called, and the oldClass is called parent, base or superclass. The next part of this lesson shows some examples of the use of inheritance in PHP.

Example 1: Simple Heritage Application

The following script shows the easy use of inheritance in a PHP script. The employee’s script contains a parent class containing two class variables, while the setData() method is used to initialize the class variables. The executive class is a subclass inherited from the employee class, which uses the extended keyword. It contains a class variable and a method called showDetails() to produce the class variables of parents and children. Once the subclass object is created, the methods of the parent and subclasses that use the subclass object are called.

// Parent school
Employee/

// Parental class variables
public $Name;
public $Department ;

// initialize the
public service master data setData()
{
$this->name = John Abraham;
$this->Department = HR;
}
}.

// Child class
Management class expands the employee [children].

// Class
small public variable $-name = Marketing Executive ;

// Perform personnel data public function showDetails(){if($this->Name != && $this->Description != && $this->Department != ){echo personnel data: ;echo Name: .$this->Name.;echo Assignment: .$this->Value.;echo Department: .$this->Department.;}}}}

//Create the object of a child class $objEmp=new Executive();$objEmp->setData();//Create the calling method of a child class $objEmp-> showDetails() ;

?>

Get off.

After executing the script, the following output appears. Here the employee name and department values are printed from the higher class, and the employee description value is printed from the lower class.

Example 2: Inheritance with transfer from method

Method neutralisation occurs when a method with the same name is reported in both the maternal and subclasses. The following script shows method override using a PHP script Here the showDetails() method is declared in both the parent and child classes. The parent class object has access to the showDetails() method of the parent class and the child class object has access to the showDetails() method of the child class.

// Parent school
Employee/

// Parental class variables
public $Name;
public $Department ;

/Initialize data
Function __construct()
{
$this->Name = Janifer Lopez;
$this->Department = Sales;
}
//Print employee data
public function showDetails()
{
echo Employee data: [From parent class]
;
echo Name: .$this->Name.
;
ultrasound department: .$this->Department.

;
}
}.

// Child class
Management class expands the employee [children].

// Class
small variable public name $ = Sales Executive ;

/Print data employee
public function showDetails()
[
echo employee details : [Children’s class]
;
echo name: .$this->name.
;
echo destination: .$this->value.
;
echo department: .$this->department.
;
}
}

// Create the parent object of class
$objEmployee=new employee() ;
// Name the parent method of class
$objEmployee->showDetails() ;

// Create an object of subclass
$objExecutive=new Executive() ;
// Call the method of subclass
at $objExecutive-> showDetails() ;

?>

Get off.

After executing the script, the following output appears. When the showDetails() method is called with the parent class object, the output of the parent class is displayed. When the showDetails() method is called with a child class object, the output of the child class is displayed.

Example 3: Call to the parent within the child builder

If both the parent class and the children’s class contain a construction method, the children’s class can call the constructor of the parent class. The following script shows how you can call the parent class builder from the child class builder’s point of view. To call the parent constructor, the parent operator::__construct() is used.

// Parents’ school
Employee/

// Parental class variables
public $ name;
public $ department ;

//Initialize data
Function __construct($name,$dept)
{
$this->name = $name;
$this->Department = $dept;
}
}

// Child class
Management class expands the employee [children].

// Child variables in class
public $ value;
public $ saltire ;

//Initialize data
Function __construct($Name,$Department,$Description,$Wage)
{
//Call constructor of parent class
older: : __construct($Name,$Department,$Department);
$thise->Description = $Description;
$thise->Salary = $Salary;
}.

// Output employee information
public function showDetails()
{
echo employee details:
;
echo name: .$this->name.
;
echo destination: .$this->value.
;
echo department: .$this->department.

Echo department: $.$-ando->payment.
;
;
;}

// Create the object of subclass
$objExecutive=new Executive (‘Jafar Iqbal’, ‘Marketing’, ‘Marketing Executive’,4500) ;
// Name the method of subclass
$objExecutive-> showDetails() ;

?>

Get off.

After executing the script, the following output appears. Here the name of the employee and the department are initialized by the superior designer, while the assignment and the salary of the employee are initialized by the subordinate designer.

Example 4: Execution of hierarchical inheritance

The following script shows how to implement hierarchical inheritance in PHP. Here class 2 is created by the inheritance of class 1 and class 3 by the inheritance of class 2. In this example, three methods are defined in three classes. Object class3 is created to invoke the methods of all classes.

// Parental class
Class 1 (

Function showMethod1()
{
echo This is the upper class
;
}
}.

// Child class
class 2 is an extension of class 1 {

Function showMethod2()
{
echo This is a subclass
;
}
}.

//Grand shield class
Extended class3 Class 2 {

Function showMethod3()
{
echo This is a large child class;
}
}.

$Object = new class3() ;

object->showMethod1();
$object->showMethod2();
$object->showMethod3();
?>.

Get off.

After executing the script, the following output appears. Here appeared the first line of text from class 1, the second line of text – from class 2, and the third line of text – from class 3.

Conclusion

This lesson shows different ways to inherit with PHP scripts. After reading this article, you should understand the concept of inheritance and its main application in PHP scripts.

Related Tags:

interface in php w3schools,php interface vs abstract class,php interface constructor,php interface type hinting,php check if class implements interface,php implements vs extends,linux signals list,linux signal 0,signals in linux geeksforgeeks,importance of linux signals,linux signal() function,about signals in linux programming,php oop example project,php classes advanced tutorial,php oop tutsplus,why do we use unlink() function in php,php call method from another class,oops concepts in php with realtime examples,php return type hint array of objects,interface php return self,php interface return type,return type this php,was expecting a return type hint,php return type declaration,php 8 benchmark,php 8 release date,laravel php 8,php jit,php 8 reddit,php 8 async,vscode-java classpath,vscode java go to definition not working,vscode-java intellisense not working,vscode-java – import cannot be resolved,java snippets vscode,vscode java auto import,php trait,php multiple inheritance,php abstract class,php implement multiple interfaces,php interfaces,php interface properties