使用特征的类的名称可以从属于该特征的静态方法中确定吗?
例如:
trait SomeAbility {
public static function theClass(){
return <name of class using the trait>;
}
}
class SomeThing {
use SomeAbility;
...
}
获取班级名称:
$class_name = SomeThing::theClass();
我的预感是,可能不会。我还没有找到任何其他暗示。
Use 后期静态绑定static https://php.net/manual/language.oop5.late-static-bindings.php:
trait SomeAbility {
public static function theClass(){
return static::class;
}
}
class SomeThing {
use SomeAbility;
}
class SomeOtherThing {
use SomeAbility;
}
var_dump(
SomeThing::theClass(),
SomeOtherThing::theClass()
);
// string(9) "SomeThing"
// string(14) "SomeOtherThing"
https://3v4l.org/mfKYM https://3v4l.org/mfKYM
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)