【发布时间】:2013-10-22 09:42:30
【问题描述】:
我在调用另一个类中的一个类的静态属性时遇到问题。
Class A {
public $property;
public function __construct( $prop ) {
$this->property = $prop;
}
public function returnValue(){
return static::$this->property;
}
}
Class B extends A {
public static $property_one = 'This is first property';
public static $property_two = 'This is second property';
}
$B = new B( 'property_one' );
$B->returnValue();
我希望返回 This is first property 但输出只是 __construct 中的参数输入的名称;
当我print_r( static::$this->property ); 时,输出只是property_one
【问题讨论】:
-
您正在实例化
A,它永远不会设置或访问$property_one。所以永远不会返回预期的结果 -
对不起,我编辑了帖子,实际上那是在实例化 B。抱歉打错了。
-
你不能在构造函数中返回值,
new B总是给你一个对象 -
我编辑了帖子,目标是我想从 returnValue 方法中获取静态 $property_one 的值。
标签: php oop static dynamic-variables late-static-binding