PHP OOP - Inheritance

PHP

In this tutorial we will learn about Inheritance in PHP.

What is Inheritance?

In Object Oriented Programming, when a class derives its properties and methods from another class then it is called Inheritance.

The class that derives properties and methods is called the child-class or the sub-class. The class from which inheritance takes place is called the super-class or the parent-class.

We use the extends keyword to inherit from a parent class.

In the following example we have the parent class Vehicle. We are creating a child class Car which is inherited from the parent class.

//parent class
class Vehicle {
	private $color;
	
	public function setColor($color) {
		$this->color = $color;
	}
	
	public function getColor() {
		return $this->color;
	}
}


//child class
class Car extends Vehicle {
	private $steeringWheel;
	
	public function setSteeringWheel($steeringWheel) {
		$this->steeringWheel = $steeringWheel;
	}
	
	public function getSteeringWheel() {
		return $this->steeringWheel;
	}
}

//object of the child class
$obj = new Car();

//set property
$obj->setColor("Black");
$obj->setSteeringWheel("Leather");

//get property
$color = $obj->getColor();
$steeringWheel = $obj->getSteeringWheel();

//this will output "Color = Black and SteeringWheel = Leather"
printf("Color = %s and SteeringWheel = %s", $color, $steeringWheel);

Properties and Methods that can be inherited

Public and Protected properties and methods can be inherited from parent class to child class.

Private properties and methods can't be inherited.

Method overriding

In OOP, when a child class has a method with the same name as the parent class it is called the overriding of the method of the parent class.

When that method is called by an object of the child class then, the child class method is executed instead of the parent class method.

In the following example we have a parent class Foo which is inherited by a child class Po. The parent class has a method hello() which is overridden in the child class Po.

//parent class
class Foo {
	
	//method
	public function display() {
		printf("class Foo method display...");
	}

	public function hello() {
		printf("class Foo method hello...");
	}

}

//child class
class Po extends Foo {
	
	//override hello - parent method
	public function hello() {
		printf("class Po method hello... method hello() overridden...");
	}
}

//object of parent class Foo
$foo_obj = new Foo();

$foo_obj->display();	//this will print "class Foo method display..."

$foo_obj->hello();		//this will print "class Foo method hello..."


//object of child class Po
$po_obj = new Po();

$po_obj->hello();		//this will print "class Po method hello... method hello() overridden..."

Calling overridden parent method

To call an overridden method of a parent class from the child class we use the parent keyword followed by the :: operator and then the method name.

//parent class
class Foo {
	public function hello() {
		printf("class Foo method hello...");
	}
}

//child class
class Po extends Foo{

	//overriding parent method
	public function hello() {
		printf("class Po method hello...");

		//calling parent method
		parent::hello();
	}
}

//creating child class object
$po_obj = new Po();

/**
 * this will print
 * "class Po method hello..."
 * "class Foo method hello..."
 */
$po_obj->hello();

Preventing class inheritance

To prevent a class from getting inherited we use the final keyword.

final class Sample {
	
}

//this will cause error - can't inherit final class
class Foo extends Sample {
	
}

Preventing method overriding

To prevent a class method from getting overridden we use the final keyword before the function.

//parent class
class Sample {
	
	//final method
	public final function hello() {

	}
}

//child class
class Po extends Sample {
	
	//this will given error - can't override final method
	public function hello() {
	
	}
}