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.

19 lines
433 B

3 years ago
  1. <?php
  2. /**
  3. * 代码复用,填补了php中继承不能多继承的缺点
  4. * 感觉就是use Cardoor 的代码等于trait Cardoor{...}的代码,有点像宏
  5. */
  6. // 1 用trait定义
  7. trait Cardoor{
  8. public function open(){
  9. echo "car door open".PHP_EOL;
  10. }
  11. }
  12. class Car{
  13. use Cardoor; //2 用use调用trait
  14. }
  15. $myCar = new Car();
  16. $myCar->open();
  17. ?>