OOP PHP Namespace, Composer dependency Manager Bangla Tutorial

Author: Al-mamun Sarkar Date: 2020-04-18 10:57:38

এই Lesson এ Object-Oriented PHP তে Namespace এর ব্যবহার এবং Composer দিয়ে কিভাবে dependency manage করতে হয় তা জানব।  

 

src/Truck.php


namespace App;

class Truck
{
	function __construct()
	{
		echo "done";
	}
}

 

src/Test/Bus.php

namespace App\Test;

use App\City\ParentCls;

class Bus extends ParentCls {
	public function test() {
		echo 'This is test';
	} 
}

 

src/Test/Vehicle.php

namespace App\Test;

class Vehicle {
	public function fuelAmount() {
		echo 12;
	}
}

 

src/City/ParentCls.php

namespace App\City;

class ParentCls {
	public function message() {
		echo "Hello";
	}
}

 

src/City/Vehicle.php

namespace App\City;

class Vehicle {
	public function fuelAmount() {
		echo 25;
	}
}

 

index.php

require 'src/Test/Vehicle.php';
require 'src/Test/Bus.php';

use src\Test\Bus;

$obj = new src\Test\Vehicle();
$obj->fuelAmount();

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

 

index2.php

require 'vendor/autoload.php';

use App\Test\Bus;

$obj = new App\Test\Vehicle();
$obj->fuelAmount();
echo "<br>";

$bus = new Bus();
$bus->test();
echo "<br>";

$bus->message();