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

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 111
    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.

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: