Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Change method visibility with traits

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 104
    Comment on it

    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)

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: