Home  >  Article  >  Backend Development  >  How to use traits in php

How to use traits in php

王林
王林forward
2021-05-04 15:00:112207browse

How to use traits in php

1. Do you know what traits are in php?

It looks like both a class and an interface, but it is actually neither.

Trait can be regarded as a partial implementation of a class, which can be mixed into one or more existing PHP classes. It has two functions: indicating what the class can do; and providing modular implementation. Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction.

2. PHP version requirements:

Traits have been introduced since php5.4, the purpose of which is to reduce code duplication and increase code reusability.

3. Trait usage scenarios:

Imagine a situation like this. When a method needs to be used in many classes, how to deal with it?

Usually the general approach is to write a base class, implement this method in the base class, and then all classes inherit this base class.

This is a way to deal with it, but it is not the best way to deal with it. Inheritance is usually used when several classes have great similarities. For example, people are a base class, and students, workers, etc. inherit the base class "people" to extend.

Thus, the role of trait comes out. Trait can be used in multiple classes.

4. How to use trait:

Quote the example in the PHP manual:

Example 1

<?php
trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}
?>

1. Declare a trait first;

2. Use use in the class to introduce the trait.

Is it very simple (manual escape)? What needs to be noted is the priority of traits.

(Free learning video sharing: php video tutorial)

5. Trait priority

(Knock on the blackboard) Members inherited from the base class Will be overwritten by members inserted by trait. The order of precedence is that members from the current class override the trait's methods, and the trait overrides the inherited methods.

Priority: own method>trait method>inherited method (this is what it looks like.)

Look at the example

<?php
trait HelloWorld {
    public function sayHello() {
        echo &#39;Hello World!&#39;;
    }
}

class TheWorldIsNotEnough {
    use HelloWorld;
    public function sayHello() {
        echo &#39;Hello Universe!&#39;;
    }
}

$o = new TheWorldIsNotEnough();
$o->sayHello();//输出是 Hello Universe!
?>

Another thing to note is : The use of multiple traits.

<?php
trait Hello {
    public function sayHello() {
        echo &#39;Hello &#39;;
    }
}

trait World {
    public function sayWorld() {
        echo &#39;World&#39;;
    }
}

class MyHelloWorld {
    use Hello, World;
    public function sayExclamationMark() {
        echo &#39;!&#39;;
    }
}

$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();
?>

Summary: Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction.

Related recommendations: php tutorial

The above is the detailed content of How to use traits in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete