Trait
类似于Class,但是比Class简单,常用于写类似插件的功能,不需要实例化,仅use就能用。如:
Trait代码
<?phpnamespace app\index\controller\test;Trait MyTrait{public static function desc(){print_r('hi i am trait');}public function whoAmI(){print_r('do not you know who you are');}}
使用:
<?phpnamespace app\index\controller\test;class Index{use MyTrait;public function index(){self::desc();$this->whoAmI();}}
结果:
hi i am traitdo not you know who you are
上一篇:
统计数组中的所有值
下一篇:
http_user_agent真实记录库
2人点赞
