Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Multiple Traits

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 116
    Comment on it

    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.

    1. trait Salary
    2. {
    3. protected $salary;
    4. public function getSalary()
    5. {
    6. return $this->salary;
    7. }
    8. public function setSalary(Salary $salary)
    9. {
    10. $this->salary = $salary ;
    11. }
    12. }
    13. trait Rollno
    14. {
    15. protected $rollno;
    16. public function getRollno()
    17. {
    18. return $this->rollno;
    19. }
    20. public function setRollno(Salary $rollno)
    21. {
    22. $this->rollno = $rollno ;
    23. }
    24. }
    25. class student
    26. {
    27. use Rollno;
    28. use Salary;
    29. }
    30. $std = new Student();
    31. $rollno = $std->getRollno();
    32. $salary = $std->getSalary();
    33. ?>

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: