PHP トレイト是什么?

特性

在类内部添加一个类,并可以重新利用其方法等。

示例代码。

<?php

class Car
{
 public function run()
 {
 echo "running by Car\n";
 }

 public function horn()
 {
 echo "beeeep!! by Car\n";
 }
}

trait Honda
{
 public function run()
 {
 parent::run();
 parent::horn();
 echo "running!! by Honda\n";
 }
}

class Machine extends Car
{
 use Honda;
}

$machine = new Machine();
$machine->run();

# running by Car
# beeeep!! by Car
# running!! by Honda



请提供以下内容的中文本地化的释义:
– Reference

广告
将在 10 秒后关闭
bannerAds