OOP PHP Multilevel Inheritance, Constructor Overriding

OOP PHP Multilevel Inheritance, Constructor Overriding

Instructor-svgAl-Mamun Sarkar
Apr 18 , 2020
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram

এই Lesson এ Object-Oriented PHP তে Multilevel Inheritance এবং Constructor Overriding নিয়ে আলোচনা করব। 

 

Vehicle.php

class Vehicle {
	
	public $capacity = 15;

	public function fuelAmount() {
		return 10;
	}

	public function capacity() {
		return $this->capacity;
	}

	public function applyBrakes() {
		return 'Braked';
	}

	public function message() {
		echo "Welcome to Vehicle <br/>";
	}
}

 

Car.php

require "Vehicle.php";

class Car extends Vehicle {

	public function display() {
		echo 'Welcome to Car';
	}

	public function fuelAmount() {
		return 20;
	}

}

 

Bus.php

require "Vehicle.php";

final class Bus extends Vehicle {

	public function display() {
		echo 'Capacity is : ' . $this->capacity . '<br>';
		$this->capacity = 40;
		echo 'Welcome to Bus';
	}

	final public function fuelAmount() {
		return 50;
	}

}


$bus = new Bus();
$bus->display();

echo "<br/>";
echo $bus->capacity();
$bus->capacity = 54;
echo "<br/>";
echo $bus->capacity();

echo "<br/>";
echo 'Fuel Amount is: ' . $bus->fuelAmount();

 

GreenBus.php


require "Bus.php";

class GreenBus extends Bus {

	public function fuelAmount() {
		return 50;
	}

}

echo "<br/>";
echo "Welcome to Green Bus Class <br/>";
$GreenBus = new GreenBus();
echo $GreenBus->fuelAmount();

 

 

Constructor Overriding:

Vechicle.php

class Vehicle {
	
	public $capacity;
	public $fuelAmount;


	public function __construct( $capacity, $fuelAmount ) {
		$this->capacity 	= $capacity;
		$this->fuelAmount 	= $fuelAmount;
	}

	public function fuelAmount() {
		return $this->fuelAmount;
	}

	public function capacity() {
		return $this->capacity;
	}

	public function applyBrakes() {
		return 'Braked';
	}
}

 

Truck.php

require "Vehicle.php";

class Truck extends Vehicle {



}


$truck = new Truck();
$truck->message();

 

Car.php

require "Vehicle.php";

class Car extends Vehicle {

	public $color;

	public function __construct( $capacity, $fuelAmount, $color ) {
		parent::__construct($capacity, $fuelAmount);
		$this->color = $color;
	}

}


$car = new Car(20, 25, 'red');
echo $car->capacity();
echo "<br/>";
echo $car->fuelAmount();
echo "<br/>";
echo 'Color is: ' . $car->color;