Inheritance is a Object-Oriented Programming (OOP) concept whereby a class can derive its properties and methods to other classes. The inheritance concept is used to take advantage of the code reuse feature to avoid duplication of program code.
The inheritance concept creates a structure or hierarchy class in the program code. Class that will be derived can be called as parent class, super class, or base class. While the class that received the decline can be referred to as child class, sub class, derived class orheir class.
Not all property and method of the parent class will be lowered. Property and method with private privileges will not be passed on to the child class. Only property and method with protected and public privileges are accessible from the child class.
In PHP, the inheritance of a class to another class uses the keyword: extends, with basic writing as follows :
parent class {
//...contents of the parent class
}
child class extends parent
{
//... child class can access
//... property and method of parent class
}
To be more easily understood, we will immediately enter into the example of inheritance use/decline program in PHP below :
<?php
// create a parent class : computer
class computer {
public $brand;
public $processor;
public $memory;
public function buy_computer() {
return "Buy a new computer";
}
}
// lower the computer class to the laptop
class laptop extends computer {
public function see_spec() {
return "brand: $this->brand, processor: $this->processor,
memory: $this->memory";
}
}
// create object from laptop class (instantiation)
$new_laptop = new laptop();
// the contents of the object property
$new_laptop ->brand = "Acer";
$new_laptop ->processor ="Intel core i5";
$new_laptop ->memory = "8 GB";
//call object method
echo $new_laptop ->buy_computer();
echo "
";
echo $new_laptop ->see_spec();
?>
Then, type the above code in the text editor.
Here are the result :
In the above code example, we create a class computer with multiple property and a method. The computer class property has no value yet.
class computer {
public $brand;
public $processor;
public $memory;
Under computer class, we create class laptop extends computer. Here we lowered the computer class into the laptop class. In the laptop class, we can access all the property and method of any class of computers (with see_spec() function) as long as they have public or protected privileges.
class laptop extends computer {
public function see_spec() {
return "brand: $this->brand, processor: $this->processor,
memory: $this->memory";
To prove it, we created a $new_laptop object from the laptop class. Note that we can access the $brand,$processor, and $memory property which are all belonging to the computer class instead of the laptop class. The buy_computer() is also successfully accessed from the $new_aptop object. This is what is meant by inheritance / drop class in OOP.
PHP does not limit how many decline object that can be done, in the following example, we create three classes that each lowered :
<?php
// create computer class
class computer {
protected function buy_computer() {
return "Buy a new computer";
}
}
// lower the computer class to the laptop
class laptop extends computer {
protected function buy_laptop() {
return "Buy a new laptop";
}
}
// lower the laptop class to the tablet
class tablet extends laptop {
protected function buy_tablet() {
return "Buy a new tablet";
}
public function buy_all(){
$a = $this->buy_computer();
$b = $this->buy_laptop();
$c = $this->buy_tablet();
return "$a
$b
$c";
}
}
// create object from laptop class (instantiation)
$new_gadget = new tablet();
//call object method
echo $new_gadget->buy_all();
// $new_gadget->buy_computer();
// Fatal error: Call to protected method computer::buy_computer()
?>
Then, type the above code in the text editor.
Here are the results :
In the example above, we create a computer class that is derived to the laptop class, and then down again to the tablet class. From within this tablet class then we call the method from the class above it.
If we note, any method other than the buy_all() method, has protected permissions. This protected right blocks other program code to access it, in addition to the derived class.
In the last line, we insert the program code to try to access the buy_computer() method. This code is deliberately flagged. If we uncommented, PHP will issue an error stating we can not access the method with permissions protected.
....
$new_gadget->buy_computer();
// Fatal error: Call to protected method computer::buy_computer()
?>
Here are the results :
This is what is meant by encapsulation in OOP. Restricting methods that should not be accessed will make the program code more structured.