Detailed PHP7 Trait

Source: https: //blog.csdn.net/qq_35255775/article/details/80610586

 

php, can not inherit from the previous to now has been the single inheritance language from the two base class properties and methods, in order to solve this problem, php7 the Trait this feature (I heard this Trait and Go language is somewhat similar, not specific Go learned language).                                                               

To use: use the keyword in the class, trait name of the statement to be combined. Statement trait specific trait keyword, trait can not be instantiated.

Code as follows:

    <PHP?
    Trait Cat {
        public Color = $ 'Black';
        public Work function () {
            echo 'and mouse', '<br>';
        }
    }
    class Animal {
        public RUN function () {
            echo ' runs fast ',' <br> ';
        }
    }
    class TmoCat the extends Animal {
        use Cat;
        public Color function () {
            echo $ this-> Color,' <br> ';


    Tom new new TmoCat = $ ();
    $ tom-> Color ();
    $ tom-> RUN ();
    $ tom-> Work ();

The results are as follows:


Test trait, the base class, this class of the same name processing properties and methods

code is as follows:

 

trait Cat {
    public Color = $ 'Black';
    public Work function () {
        echo 'and mouse', '<br>';
    }
    public rUN function () {
        echo 'cat runs fast', '< br> ';
    }
}
class Animal {
    public Color = $' Color ';
    public Work function () {
        echo' their duties ',' <br> ';
    }
    public rUN function () {
        echo' runs fast ',' <br> ';
    }
}

class TmoCat extends Animal{
    use Cat;
    Color = $ public 'White-Black';
    public Work function () {
        echo $ this-> Color, '<br>';
    }
}

$ = new new Tom TmoCat ();

$ tom-> RUN ();
$ tom- > work ();

if this class defines the base class, or the same trait will be given attributes, given the information shown below, the base class for this class and can not be defined with the same properties as the trait.

Modify the code:

<PHP?
Trait Cat {
    public Color = $ 'Black';
    public Work function () {
        echo 'and mouse', '<br>';
    }
    public RUN function () {
        echo 'cat runs fast' , '<br>';
    }
}
class Animal {
    public function Work () {
        echo 'their duties', '<br>';




}


Class TmoCat the extends Animal {
    use Cat;
    public Work function () {
        echo 'catch Jack', '<br>';
    }
}

$ = new new Tom TmoCat ();

$ tom-> RUN ();
$ tom-> Work ();

the results are as follows:

the base class can cover the visible trait, the trait of the present class methods can cover.

A plurality of classes may be combined trait

a trait plurality of classes may be combined, separated by commas plurality trait. For example use trait1, trait2;

code shows:

    <PHP?
    Trait Cat {
        public Color = $ 'Black';
        public Work function () {
            echo 'and mouse', '<br>';
        }
    }
    trait Tiger {
        public function EAT ( ) {
            echo 'tiger meat', '<br>';



        RUN function public () {
            echo 'runs fast', '<br>';
        }
    }
    class TmoCat the extends Animal {
        use Cat, Tiger;
        public Color function () {
            echo $ this-> Color, '<br>' ;
        }
    }
    $ = new new Tom TmoCat ();
    $ tom-> Color ();
    $ tom-> RUN ();
    $ tom-> Work ();
    $ tom-> EAT ();

The results are as follows:


If two triat have the same properties and methods, then there is at the same time use, how to distinguish?

When there are different trait or property in the same manner it will conflict, or the solution is used insteadof as to resolve. insteadof is an alternative, as it is for aliasing.

Code is as follows:

<PHP?
Trait trait1 {
    public function Drive () {
        echo 'This IS trait1 Drive', '<br>';


    public function color(){
        echo 'This is trait1 color','<br>';
    }

}

trait trait2{
    public function drive(){
        echo 'This is trait2 drive','<br>';
    }

    public function color(){
        echo 'This is trait2 color','<br>';
    }
}

class Car{
    use trait1,trait2{
        trait1::drive insteadof trait2;
        trait1::color insteadof trait2;
    }
}

class Bus{
    use trait1,trait2{
        trait1::drive insteadof trait2;
        trait1::color insteadof trait2;
        trait2::color as color2;
        trait2::drive as drive2;
    }
}

class Bike{
    use trait1,trait2{
        trait1::color insteadof trait2;
        trait2::color as color1;
        trait2::drive insteadof trait1;
        trait1::drive as drive2;
    }
}

$car = new Car();
$car->color();
$car->drive();
echo '-------------','<br>';
$bus = new Bus();
$bus->drive();
$bus->color();
$bus->color2();
$bus->drive2();
echo '-------------','<br>';
$bike = new Bike();
$bike->color();
$bike->color1();
$bike->drive();
$bike->drive2();

结果如下:


Note: two identical properties or methods must be replaced by another one of the attributes and methods to use insteadof, if not replaced will define other properties or methods will be given alias.

The method can also be modified as access control

<PHP?
trait Animal {
    public EAT function () {
        echo 'Animal CAN EAT';
    }
}

class Cat {
    use {Animal
        Animal :: EAT as protected;
    }
}

class Dog {
    use Animal {
        Private eat2 :: EAT aS Animal;
    }
}

$ = new new CAT Cat ();
$ cat-> EAT (); // can not output because it has been modified to properly protected to EAT

$ = new new Dog Dog ();
$ DOG> eat (); // type eat normal output is not modified
$ dog-> eat2 (); // can not be normally output type is eat2 Private

trait can also be combined with each other, using the abstract methods, static methods, static properties

Code as follows:

? <PHP
trait Animal {
    public static $ a_sta = 'Animal static';
    public a_not_sta $ = 'Animal nonstatic';
    abstract public function a_name ();
    public static a_eat function () {
        echo 'static method Animal', '<br>';
    }
}

trait {Tiger
    use Animal;
    public static t_sta $ = 'Tiger static';
    public t_not_sta $ = 'Tiger nonstatic';
    abstract public function name ();
    public static t_eat function () {
        echo 'Tiger static method', '<br> ';
    }
}

class Cat {
    use Tiger;

    public function name ()
    {
        // the TODO: Implement name () method.
        echo' Tiger abstract method ',' <br>';
    }

    a_name function public ()
    {
        // the TODO: Implement a_name () Method.
        echo 'Animal abstract method', '<br>';
    }

    public function Show () {
        echo 'Animal static properties:', self :: $ a_sta, '<br>';
        echo 'Animal non-static properties:', $ this-> a_not_sta, '<br>';
        echo 'Tiger static properties:', Self :: $ t_sta, '<br>';
        echo 'Tiger non-static property: ', $ this-> t_not_sta,' <br> ';
    }
}

$ = new new CAT Cat ();
$ cat-> name ();
$ cat-> a_name ();
Cat :: a_eat () ;
Cat :: t_eat ();
$ cat-> Show ();

results are as follows

 

Guess you like

Origin www.cnblogs.com/laijinquan/p/10992406.html