CLASS web

PHP

trait as

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();
?>

I'll check the results right below.







Result

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





ALL COMMENTS 0

Sort by

PinkCoding

PinkCoding

X

PinkCoding

Web Scratch Pad

X

loading

            
            
        

Back to the course