How to use traits in php? (with examples)

不言
Release: 2023-04-04 15:04:01
Original
3436 people have browsed it

Since PHP5.4.0, PHP has implemented a code reuse method called trait. In order to actually use the trait, the trait is the same as the class name. You first need to specify the trait name. In the defined trait module, you can define methods. Let's take a look at the details of this article.

How to use traits in php? (with examples)

The tasks required to create a trait are the above "determine the trait name" and "define the required method".

Let’s take a look at how to use trait

Definition of trait

trait  特征名{
    function 方法名1() {
   }

    function 方法名2() {
    }
}
Copy after login

Use of trait

class 类名 {
    // 这使得类与定义方法1和方法2的状态相同
    use trait名;
}
Copy after login

Specific example

In the following code, we have prepared the book class and pen class, and there is a calculation in both classes The price process includes common taxes, so we define this process with traits.

I think it is possible to illustrate that the tax-included calculation function can be used by simply writing "use TaxCalculator;"

If this value is defined in the book class/pen class, the amount of code to be written will increase, and both classes must be modified when making corrections.

Using traits will reduce the amount of code, and even if a fix occurs, the maintainability is high because it only needs to fix the TaxCalculator.

// 税的计算处理
trait TaxCalculator {
    private $price;         // 价格
    private $tax = 0.08;    // 税收
    // 返还含税的价格
    public function taxIncluded() {
        return $this->price * (1 + $this->tax);
    }
}
// 表示book类的信息
class Book {
    use TaxCalculator;
    public $title;      // 标题
    public $author;     // 作者
    public function __construct($price, $title, $author) {
        $this->price = $price;
        $this->title = $title;
        $this->author = $author;
    }
}
// 表示pen类的信息
class Pen {
    use TaxCalculator;
    public $color;  // 颜色
    public $type;   // 自动笔或者铅笔

    public function __construct($price, $color, $type) {
        $this->price = $price;
        $this->color = $color;
        $this->type = $type;
    }
}
// 把书和笔实例化
$book = new Book(80, ""红楼梦"", ""曹雪芹"");
$pen = new Pen(10, ""black"", ""sharp"");

// 输出含税的价格
echo $book->taxIncluded().PHP_EOL;  // 324
echo $pen->taxIncluded().PHP_EOL;   // 108
Copy after login

This article ends here. For more exciting content, you can pay attention to the relevant tutorial columns of the How to use traits in php? (with examples) Chinese website! ! !

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!