PHP抽象类访问下面的常量
在PHP中,抽象类是否可以访问下面类的常量 例如,我可以在泛型中分解getName吗PHP抽象类访问下面的常量,php,class,static,abstract,factorization,Php,Class,Static,Abstract,Factorization,在PHP中,抽象类是否可以访问下面类的常量 例如,我可以在泛型中分解getName吗 abstract class Generic { abstract public function getName(): string; } class MorePreciseA extends Generic { private const NAME = "More Precise A"; public function getName(): string {
abstract class Generic {
abstract public function getName(): string;
}
class MorePreciseA extends Generic {
private const NAME = "More Precise A";
public function getName(): string {
return self::NAME;
}
}
class MorePreciseB extends Generic {
private const NAME = "More Precise B";
public function getName(): string {
return self::NAME;
}
}
谢谢这就是
自我:
和静态:
之间的区别所在。更多关于这方面的信息可以找到
将导致
// string(7) "Generic"
// string(7) "Generic"
但是如果您像这样替换通用实现
abstract class Generic {
public function getName(): string {
return static::NAME;
}
}
然后它将输出
// string(14) "More Precise A"
// string(14) "More Precise B"
不管它是抽象的,父类没有办法知道关于子类属性的任何信息(即使它不是私有的)。你能详细说明一下你想要达到的目标吗?也许我们可以建议不同的设计。完美,正是我想要的。谢谢
// string(14) "More Precise A"
// string(14) "More Precise B"