php trait 变量类型为数组时 不能被父类子类同时use

直接上代码

---------------------------

trait T1 {

public static $a=1;

public static $b= [];

public static function getC(){

echo 'class: ' . get_class() . "\n";

}

}

class a{

use T1;

}

class b extends a{

use T1;

public static function getHw(){

echo 'Hello';

}

}

b::getHw();

--------------

运行以上代码,错误信息

Fatal error: a and T1 define the same property ($b) in the composition of b. However, the definition differs and is considered incompatible. Class was composed

---------------

此时把

public static $b= [];改为public static $b= 1;非数组的其他类型正常运行。

---------------

至于父类和子类都use T1,是为了让b::getC();取到的类名是b而不是a

上一篇:DOM基础:table(表格)


下一篇:Java进击C#——应用开发之Linq和EF