QUIZGUM

Coding Class

Quizgum : trait

trait

No inheritance is necessary.
You and I are growing up in a horizontal relationship, and I'm old and employed so I can organize my inheritance with my parents and stand up independently.
The expression is strange.
In order to use certain features in a class, you must maintain inheritance with the parent class.
But there is a trait that is not the inheritance, but the concept of bringing.
In other words, think of it as a concept of inserting you into me without inheriting between classes.
Another class cannot inherit from two parent classes.
Next, please check the class.

So let's look at an example to see if it can be inherited from more than one class.

<?php
    class Parent
    {
    }

    class Parent2
    {
    }

    class mickey extends Parent, Prent2
    {
    }

    $mickey = new mickey;
?>

As you can see, it's impossible.
Multiple traits can be used. And it's an insertion concept, not an inheritance concept.
If you're not on top of me, you're coming to me for a while.
Using traits is similar to class, but you can't create instances of traits, but you can use trait methods through classes.

How To Use trait

trait traitname{}

And to use traits in a class, use the use keyword.

How to insert a trait

trait traitname{}
class classname
{
    use traitname;
}

Then let's declare the method in the trait and call it.

<?php
    trait disney
    {
        public function mickey()
        {
            return "Disney's beginning Mickey Mouse";
        }
    }

    class marvel
    {
        use disney;
    }

    $marvel = new marvel;
    echo $marvel->mickey();
?>

You can also use multiple traits in one class.

<?php
    trait disney
    {
        public function mickey()
        {
            return "Disney's beginning Mickey Mouse";
        }
    }

    trait TokyoDisneyLand
    {
        public function duffyAndFriends()
        {
            return "We make our own character Duffy and export it to Disney in another country";
        }
    }

    trait HongkongDisneyLand
    {
        public function duffyhk()
        {
            return "Thanks TDL Hong Kong children are very fond of Duffy";
        }
    }

    class marvel
    {
        use disney, TokyoDisneyLand, HongkongDisneyLand;
    }

    $marvel = new marvel;
    echo $marvel->mickey();
    echo '<br>';
    echo $marvel->duffyAndFriends();
    echo '<br>';
    echo $marvel->duffyhk();
?>

Of course, you can also use properties in traits.

<?php
    trait disney
    {
        public $disney = 'disney';
        public function mickey()
        {
            return "Disney's beginning Mickey Mouse";
        }
    }

    class marvel
    {
        use disney;
    }

    $marvel = new marvel;
    echo $marvel->mickey();
    echo '<br>';
    echo $marvel->disney;
?>

Now, we've learned about traits that can be used as concepts of insertion rather than inheritance.
If you have to use a lot of things, inheritance is better than class. If you insert multiple traits and have the same method name, an error occurs.
Next time, let's find out what to do.