PHP tutorial: class inheritance


OOP in PHP

Class inheritance

The most importance advantage of OOP is reusability and extendability. By using the inheritance, you will be able to reuse the original features of the super class and add more features to the class without modifying it. The new features are defined in the derived classes. To do so you need to use the extends keyword.

<?php

class People{
 protected $name;
 protected $sex;
 function People($name,$sex){
  $this->name=$name;
  $this->sex=$sex;
 }
}

class Employee extends People{
 private $id;
 private $salary;
 function Employee($id,$name,$sex,$salary){
  $this->id=$id;
  $this->name=$name;
  $this->sex=$sex;
  $this->salary=$salary;
}

function printInfo(){
  echo "Id=".$this->id."<br />";
  echo "Salary=".$this->salary."<br />";
  echo "Name=".$this->name."<br />";
  echo "Sex=".$this->sex."<br />";
  }
}
$emp=new Employee("2233","Dara","M",40000);
$emp->printInfo();

?>


Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.