Welcome to Findnerd. We have discussed the traits in previous blogs. You can check the other blogs for basic understanding. We can use the methods including in traits
and can perform the other operations as well. You can change the methods visibility like public to private and public to protected as your requirement.
<?php
trait Salary
{
protected $salary;
public function setSalary(Salary $salary){
$this->salary = $salary;
}
public function getSalary(){
return $this->salary;
}
}
class Emp
{
use Salary { setSalary as private; getSalary as protected; }
}
?>
In above example we created two methods getSalary and setSalary and we are using Salary trait in class Emp , we changed the visibility of setSalary method to private
and getSalary method to protected.Hope this helps.
0 Comment(s)