happy cat image

everdevel

coding

login
NotificationX
  • Currently, only notices of comments are displayed.
  • no comment or please login

Create aliases for trait methods

You can create aliases for trait methods.
The keyword uses as.

how to use as

trait trait name{}
class class name
{
    use trait name, trait name {
        trait name::method name as alias;
    }
}

So let's look at an example.

<?php
    trait apple
    {
        public function phone()
        {
            return 'iPhone';
        }
    }

    trait google
    {
        public function phone()
        {
            return 'pixel';
        }
    }

    class people
    {
        use apple, google {
            apple::phone insteadof google;
            google::phone as gp;
        }
    }

    $people = new people;
    echo "Judith in zootopia ".$people->phone()." Use it.";
    echo '<br>';
    echo "Reference Android phone made by Google ".$people->gp();
?>

Result

We've learned how to create aliases for trait's methods. ^^


Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.


    
    

Back to the course

ALL COMMENTS 0

Sort by