OOP PHP Inheritance, Method Overriding In Bangla

Author: Al-mamun Sarkar Date: 2020-04-18 14:39:21

এই Lesson এ Object-Oriented PHP তে Inheritance এবং Method 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/>";
	}
}

 

Truck.php

require "Vehicle.php";

class Truck extends Vehicle {

	public function message() {
		parent::message();
		echo "This is Truck <br/>";
	}

}


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

 

Car.php

require "Vehicle.php";

class Car extends Vehicle {

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

	public function fuelAmount() {
		return 20;
	}

}


// $car = new Car();
// echo $car->applyBrakes();
// echo "<br/>";
// $car->display();

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

 

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();

 

NewCar.php

require "Car.php";

class NewCar extends Car {

}


$newCar = new NewCar();
echo $newCar->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();