What is the usage of php trait?

藏色散人
Release: 2023-03-09 18:40:01
Original
1868 people have browsed it

php trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction. Its usage syntax is such as "trait ezcReflectionReturnInfo {function getReturnType() {/*1* /}...}".

What is the usage of php trait?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

1. What is the trait 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 the class, which can be mixed into one or more existing PHP classes. It has two functions: indicating what the class can do. ;Provide 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:

PHP5.4 began to introduce traits, whose purpose 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.

From this, 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;
    /* ... */
}
?>
Copy after login

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.

5. Trait priority

(Knock on the blackboard) Members inherited from the base class will be overridden by members inserted by the 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 (that’s 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!
?>
Copy after login

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

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

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the usage of php trait?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template