[英]Accessing child property from Parent class in php
我来自java背景,最近在PHP中测试了一些OOP方法。 我发现父类允许使用Child类属性。 下面的代码描述了我想说的内容。
<?php
class ParentClass {
public static function test() {
echo "hello world ". implode(',', static::$prop);
}
}
class ChildClass extends ParentClass {
public static $prop = ['a' , 'b'];
}
ChildClass::test();
?>
在上面的代码中,ParentClass使用函数test()
的$ prop变量。 这只是输出而没有错误。 但是,Java中不允许这样做。 这种方法在PHP中背后的原因是什么?
Java没有Late static bindings的概念:
“后期绑定”来自于以下事实:static ::不会使用定义方法的类来解析,而是使用运行时信息来计算。
如果你使用self::
你将拥有Java中所知的行为。 但是static::
在运行时解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.