码迷,mamicode.com
首页 > Web开发 > 详细

php新特性:trait 关键字使用

时间:2018-04-01 00:00:53      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:size   关键字   model   输出   col   trait   font   div   require   

1、trait关键字:含义【特性】

  1.1 和require include 区别: 后两者需要 实例化一个类或者静态调用,而trait相当于继承,但又不是extends关键字,它解决了单继承。

2、目的:解决php的单继承问题

3、使用关键词【use】在控制器或者其他model类引入,如:

<?php
trait KK
{
  public $age="11";  
  
  public function say()
  {
     echo "hello ";
  }   
}

class A
{
  use kk;
  public $name="ss";  
  
  public function do()
  {
     echo "world ";
  }        

}    

//使用实例
$a = new A();
$a->say;
$a->do;

输出:hello world

 

php新特性:trait 关键字使用

标签:size   关键字   model   输出   col   trait   font   div   require   

原文地址:https://www.cnblogs.com/xuzhengzong/p/8684615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!