first of all, the concept of trait in PHP is the concept of struct anonymous field in the Go language. is the desire to incorporate one structure into another, adding functionality to another structure. Rather than through tree-shaped inheritance
PHP Trait Demo
<?php
class Base {public
function SayHello () {
echo ' Hello ';
}
}
Trait SayHello {public
function SayHello () {
Parent::sayhello ();
echo ' World! ';
}
Public Function Sayhi () {
echo ' sayhi ';
}
}
Class Myhelloworld extends Base {use
sayhello;
}
$helloWorld = new Myhelloworld ();
$helloWorld->sayhello ();
$helloWorld->sayhi ();
# #输出结果
Hello World!sayhi
# #说明用trait定义的结构可以增强其他结构的功能
Go language struct anonymous field
package main import "FMT" type Color struct {red string yellow string Blue string} Type iphone struct {Color//anonymous field, in order to enhance the iphone this data structure of size float32 memory string} func main () {Iphone: = iphone{color{"Red", "yellow", "Blue"}, 4.8, "256G"} fmt. Println (iphone.red, Iphone.yellow, Iphone.blue, Iphone.size, iphone.memory)}