You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
433 B

<?php
/**
* 代码复用,填补了php中继承不能多继承的缺点
* 感觉就是use Cardoor 的代码等于trait Cardoor{...}的代码,有点像宏
*/
// 1 用trait定义
trait Cardoor{
public function open(){
echo "car door open".PHP_EOL;
}
}
class Car{
use Cardoor; //2 用use调用trait
}
$myCar = new Car();
$myCar->open();
?>