Welcome to Findnerd. Today we are going to discuss the multiple traits in php. Trait is also a classes, only difference is that we can not create object for traits. We can only use the methods which are implemented in it via use keyword. Please check the example with multiple traits.
trait Salary
{
protected $salary;
public function getSalary()
{
return $this->salary;
}
public function setSalary(Salary $salary)
{
$this->salary = $salary ;
}
}
trait Rollno
{
protected $rollno;
public function getRollno()
{
return $this->rollno;
}
public function setRollno(Salary $rollno)
{
$this->rollno = $rollno ;
}
}
class student
{
use Rollno;
use Salary;
}
$std = new Student();
$rollno = $std->getRollno();
$salary = $std->getSalary();
?>
We can see in above example we created two traits named Rollno and Salary. We are using in the class named student.
0 Comment(s)