PHP Access Modifiers, Constant, Static Methods and Properties Bangla tutorial

Author: Al-mamun Sarkar Date: 2020-04-18 10:45:48

এই Lesson এ Object-Oriented PHP তে Private, Public, Protected Access Modifiers, Constant, Static Methods and Properties নিয়ে আলোচনা করব। 

 

Constant.php

class Constant {
	const PI = 3.1416;

	public function area( $r ) {
		return self::PI * $r * $r;
		// return Constant::PI * $r * $r;
	}
}

echo Constant::PI;
echo '<br/>';

$con = new Constant();
echo $con->area(2);

 

private.php

class PrivateClass {

	private $result = 25;

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

}


$obj = new PrivateClass();
echo $obj->getResult();

 

Static.php

class Person {
	public static $name = "Muhamad";
	public static function display() {
		echo 'Welcome to dhaka';
	}
	public function getName() {
		return self::$name;
	}
}

Person::display();
echo "<br>";
echo Person::$name;
echo "<br>";

$obj = new Person();
$obj2 = new Person();

echo 'From Object 1 : ' . $obj->getName();
echo "<br>";
echo 'From Object 2 : ' .$obj2->getName();
echo "<br>";

Person::$name = 'Sarkar';

echo 'From Object 1 : ' . $obj->getName();
echo "<br>";
echo 'From Object 2 : ' .$obj2->getName();

 

Vehicle.php

class Vehicle {
	protected $capacity = 15;

	public function getCapcity() {
		return $this->capacity . ' kg';
	}
}

class Bus extends Vehicle {
	public function updateCapacity() {
		$this->capacity = 25;
	}
}

$bus = new Bus();
echo $bus->getCapcity();
echo '<br/>';

$bus->updateCapacity();
echo $bus->getCapcity();
echo '<br/>';

echo $bus->capacity;