代码语言:javascript复制
<?php
header('Content-Type:text/html;charset=utf-8');
trait tA
{
public function aa()
{
echo "<p>aaa....</p>";
}
public function bb(){
echo "<p>bb...</p>";
}
}
abstract class Humanity
{
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE="地球";
static public $counter=0;
public function __construct($name,$sex)
{
self::$counter ;
$this->name=name;
$this->sex=$sex;
}
protected function chew($food)
{
echo "<p>{$food}已经被咀嚼完成!</p>";
}
abstract public function eat($food);
static public function hello()
{
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
}
class Student extends Humanity
{
const BIRTHPLACE='火星';
public $studentId;
public function test($subject)
{
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food)
{
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
use tA;//相当于
/*public function aa()
{
echo "<p>aaa....</p>";
}
public function bb(){
echo "<p>bb...</p>";
}*/
}
abstract class Dog
{
}
class huskies extends Dog
{
use tA;
//相当于
/*public function aa()
{
echo "<p>aaa....</p>";
}
public function bb(){
echo "<p>bb...</p>";
}*/
}
$hanMM=new Student('韩梅梅','女');
$hanMM->aa();
$wW=new Huskies();
$wW->bb();
解析:
use tA;
相当于
public function aa()
{
echo "<p>aaa....</p>";
}
public function bb(){
echo "<p>bb...</p>";
}
traits技术的基本使用技巧是:他是作用的引入一段《《《代码》》》
。
效果图:
优先级:
代码语言:javascript复制<?php
header('Content-Type:text/html;charset=utf-8');
//traits优先级
trait tA {
public function aa(){
echo "<p>tA:aa...</p>";
}
public function bb(){
echo "<p>tA:bb...</p>";
}
}
//抽象类
abstract class Humanity {
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE='地球';
static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系
//构造函数
public function __construct($name,$sex){
self::$counter ;
$this->name=$name;
$this->sex=$sex;
}
protected function chew($food){
echo "<p>{$food}已经被咀嚼完成!</p>";
}
//抽象方法
abstract public function eat($food);
static public function hello(){
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
public function aa(){
echo "<p>Humanity:aa...</p>";
}
}
class Student extends Humanity {
const BIRTHPLACE='火星';
public $studentId;
public function test($subject){
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food){
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
public function aa(){
echo "<p>Student:aa...</p>";
}
use tA;
}
abstract class Dog {
}
class Huskies extends Dog {
use tA;
}
$hanMM=new Student('韩梅梅','女');
$hanMM->aa();
解析:
本类定义的优先级比语法糖traits定义的优先级高,为什么呢? 因为有一定的层级关系:
代码语言:javascript复制父类里面的同名方法<traits里面的同名方法<本类里面的同名方法
一个文件定义多个语法糖:
代码语言:javascript复制<?php
header('Content-Type:text/html;charset=utf-8');
//traits优先级
trait tA {
public function aa(){
echo "<p>tA:aa...</p>";
}
public function bb(){
echo "<p>tA:bb...</p>";
}
}
trait tB
{
public function cc(){
echo "<p>tB:cc...</p>";
}
public function dd(){
echo "<p>tB:dd...</p>";
}
}
//抽象类
abstract class Humanity {
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE='地球';
static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系
//构造函数
public function __construct($name,$sex){
self::$counter ;
$this->name=$name;
$this->sex=$sex;
}
protected function chew($food){
echo "<p>{$food}已经被咀嚼完成!</p>";
}
//抽象方法
abstract public function eat($food);
static public function hello(){
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
public function aa(){
echo "<p>Humanity:aa...</p>";
}
}
class Student extends Humanity {
const BIRTHPLACE='火星';
public $studentId;
public function test($subject){
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food){
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
public function aa(){
echo "<p>Student:aa...</p>";
}
use tA,tB;
}
abstract class Dog {
}
class Huskies extends Dog {
use tA;
}
$hanMM=new Student('韩梅梅','女');
$hanMM->aa();
代码语言:javascript复制问题:
一次性使用多个trait,如果不提trait里面有同名的方法怎么解决这个冲突呢?
代码语言:javascript复制<?php
header('Content-Type:text/html;charset=utf-8');
//traits优先级
trait tA {
public function aa(){
echo "<p>tA:aa...</p>";
}
public function bb(){
echo "<p>tA:bb...</p>";
}
public function ok(){
echo "<p>tA:ok...</p>";
}
}
trait tB {
public function cc(){
echo "<p>tB:cc...</p>";
}
public function dd(){
echo "<p>tB:dd...</p>";
}
public function ok(){
echo "<p>tB:ok...</p>";
}
}
//抽象类
abstract class Humanity {
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE='地球';
static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系
//构造函数
public function __construct($name,$sex){
self::$counter ;
$this->name=$name;
$this->sex=$sex;
}
protected function chew($food){
echo "<p>{$food}已经被咀嚼完成!</p>";
}
//抽象方法
abstract public function eat($food);
static public function hello(){
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
public function aa(){
echo "<p>Humanity:aa...</p>";
}
}
class Student extends Humanity {
const BIRTHPLACE='火星';
public $studentId;
public function test($subject){
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food){
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
public function aa(){
echo "<p>Student:aa...</p>";
}
use tA,tB
{
tA::ok insteadof tB;
tB::ok as okTb;
}
}
abstract class Dog {
}
class Huskies extends Dog {
use tA,tB
{
tB::ok insteadof tA;
}
}
$hanMM=new Student('韩梅梅','女');
$hanMM->ok();
$hanMM->okTb();
$wW=new Huskies();
$wW->ok();
解析一:
代码语言:javascript复制use tA,tB
{
tA::ok insteadof tB;
tB::ok as okTb;
}
代码语言:javascript复制tA里面的ok方法替换掉tB里面的ok方法,tB里面的ok函数改成okTb函数
解析二:
代码语言:javascript复制use tA,tB
{
tB::ok insteadof tA;
}
tB里面的ok方法替换掉tA里面的ok方法