分享学习心得,记录学习笔记
文章:84   访问:526146
今天是:
2025年09月09日 星期二


Trait

类似于Class,但是比Class简单,常用于写类似插件的功能,不需要实例化,仅use就能用。如:

Trait代码

  1. <?php
  2. namespace app\index\controller\test;
  3. Trait MyTrait
  4. {
  5. public static function desc()
  6. {
  7. print_r('hi i am trait');
  8. }
  9. public function whoAmI()
  10. {
  11. print_r('do not you know who you are');
  12. }
  13. }

使用:

  1. <?php
  2. namespace app\index\controller\test;
  3. class Index
  4. {
  5. use MyTrait;
  6. public function index()
  7. {
  8. self::desc();
  9. $this->whoAmI();
  10. }
  11. }

结果:

  1. hi i am trait
  2. do not you know who you are


2人点赞
fuyelk
fuyelk

  

2020-03-30 23:23:51
# 1

友情链接 doywb

2018-2025 Copyright© 米灵尔 浮叶蓝空 豫ICP备15007436号-1

豫公网安备 41152302000146号