[英]Call static properties within another class in php
I have problem about calling a static property of a class inside another class. 我在调用另一个类中的某个类的静态属性时遇到问题。
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();
I expect to return This is first property
But the Output is just the name a parameter input in __construct; 我希望返回This is first property
但是Output只是在__construct中输入的参数名称;
When I print_r( static::$this->property );
当我print_r( static::$this->property );
the output is just property_one
输出只是property_one
Maybe like this? 也许是这样吗?
<?php
Class A {
public $property;
public function __construct( $prop ) {
$this->property = $prop;
print 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' );
(I mean you can access (print,...) the property this way, but the constructor will return an object anyway.) (我的意思是,您可以通过这种方式访问(打印,...)该属性,但是构造函数仍然会返回一个对象。)
Just change: 只是改变:
return static::$this->property;
with: 与:
return static::${$this->property};
There are several issues here: 这里有几个问题:
$property_one
is declared in class B
, the A
class's constructor won't have access to that property, nor can you guarantee this property to be present. 静态属性$property_one
在类B
声明, A
类的构造函数将无法访问该属性,也不能保证该属性存在。 $this->property
happens to be assigned. 当然,自PHP 5.3起,就支持后期静态绑定,但这并不能改变这样一个事实,即您永远不会确保某些静态属性被恰好称为$this->property
分配。 What if it's assigned an object? 如果分配了对象怎么办? an int, or float? 一个int还是float? static::$propery
or self::$property
. 您可以像这样访问静态属性: static::$propery
或self::$property
。 Note the $
! 注意$
! When you write static::$this->property
, you're expecting this to evaluate to self::property_one
. 当您编写static::$this->property
,您希望它的计算结果为self::property_one
。 You're clearly missing the $
sign. 您显然缺少$
符号。 self::${$this->property}
. 您至少需要的是self::${$this->property}
。 Check the PHP manual on variable variables. 查看有关变量变量的PHP手册。 To have access to a static property of a child class in the constructor, you can't but rely on the child's constructor: 要访问构造函数中子类的静态属性,您只能依靠子构造函数:
Class A
{
public $property;
}
Class B extends A
{
public static $property_one = 'This is first property';
public static $property_two = 'This is second property';
public function __construct( $prop )
{
$this->property = $prop;
print self::${$this->property};
}
}
$B = new B( 'property_one' );
An alternative would be: 一种替代方法是:
Class A
{
public $property;
public function __constructor($prop)
{
$this->property = $prop;
}
public function getProp()
{
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->getProp();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.