Object Oriented PHP Trait এর ব্যবহার

Author: Al-mamun Sarkar Date: 2020-04-18 14:52:20

এই Lesson এ Object-Oriented PHP তে  কিভাবে Trait ব্যবহার করতে হয় এবং trait ব্যবহারের সুবিধা কি তা আলোচনা করব। 

 

ParentCls.php

class ParentCls {
	public function ok() {
		echo 'Good Morning';
	}
}

 

Foo.php

trait Foo {
	public function display() {
		echo 'Welcome to foo';
	}
}

 

Bar.php

trait Bar {
	public function message(){
		echo "How are you";
	}
}

 

MyCls.php

require "ParentCls.php";
require "Foo.php";
require "Bar.php";


class MyCls extends ParentCls {
	use Foo, Bar;

	public function hello() {
		$this->message();
	}
}

$obj = new MyCls();
$obj->hello();