在另一个类的变量中调用一个类的常量
我想知道 PHP 是否有可能执行以下操作;
<?php
class boo {
static public $myVariable;
public function __construct ($variable) {
self::$myVariable = $variable;
}
}
class foo {
public $firstVar;
public $secondVar;
public $anotherClass;
public function __construct($configArray) {
$this->firstVar = $configArray['firstVal'];
$this->secondVar= $configArray['secondVar'];
$this->anotherClass= new boo($configArray['thirdVal']);
}
}
$classFoo = new foo (array('firstVal'=>'1st Value', 'secondVar'=>'2nd Value', 'thirdVal'=>'Hello World',));
echo $classFoo->anotherClass::$myVariable;
?>
预期输出: Hello World
我收到以下错误; 解析错误:语法错误,意外的T_PAAMAYIM_NEKUDOTAYIM
我用Google搜索了一下,它与$classFoo->anotherClass::$myVariable中的冒号(双点)有关code>
我不想费尽心思去改变我的其他类。无论如何,这个问题有解决办法吗?
提前感谢您的帮助。
PS 我只是不想为此浪费几个小时来寻找解决方法。昨天我已经花了 2.5 个小时来更改几乎整个 Jquery,因为客户想要更改,今天早上我被要求收回更改,因为他们不想使用它(他们改变了主意)。我现在只是想避免发生重大变化。
I was wondering if there is any possibility in PHP to do following;
<?php
class boo {
static public $myVariable;
public function __construct ($variable) {
self::$myVariable = $variable;
}
}
class foo {
public $firstVar;
public $secondVar;
public $anotherClass;
public function __construct($configArray) {
$this->firstVar = $configArray['firstVal'];
$this->secondVar= $configArray['secondVar'];
$this->anotherClass= new boo($configArray['thirdVal']);
}
}
$classFoo = new foo (array('firstVal'=>'1st Value', 'secondVar'=>'2nd Value', 'thirdVal'=>'Hello World',));
echo $classFoo->anotherClass::$myVariable;
?>
Expected OUTPUT : Hello World
I am getting following error; Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
I Googled and it is related to colon (double dots) in $classFoo->anotherClass::$myVariable
I wouldn't like to go all the trouble to change my other classes. Is there anyway around this problem?
Thank you for your help in advance.
P.S. I just didn't want to lose few hours on this to find a way around. I already spent yesterday 2.5 hours to change almost whole Jquery because customer wanted a change and today in the morning I was asked to take the changes back because they didn't want to use it (they changed their mind). I am just trying to avoid big changes right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要执行以下操作:
不支持将表达式扩展为静态调用/常量的类名/对象(但支持扩展变量,如上所示)。
You need to do:
Expanding expressions to class names/objects for static calls/constants is not supported (but expanding variables, as shown above, is).
如果您不关心内存和执行速度,这是正确的。
看来参考会更好:
对我有用。
If you do not care about memory and execution speed, this is correct.
It seems that reference would be better:
Works for me.