Rev*_*ant 4 php variables static class constants
我想知道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;
?>
Run Code Online (Sandbox Code Playgroud)
预期产量: Hello World
我收到了以下错误; Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
我用谷歌搜索,它与结肠(双点)有关$classFoo->anotherClass::$myVariable
我不想麻烦改变我的其他课程.反正这个问题呢?
提前谢谢你的帮助.
PS我只是不想在这方面浪费几个小时才找到方法.我已经花了2.5个小时来改变几乎整个Jquery,因为客户想要改变,今天早上我被要求接受更改,因为他们不想使用它(他们改变主意).我现在只是想避免重大改变.
net*_*der 11
你需要这样做:
$anotherClass = $classFoo->anotherClass;
echo $anotherClass::$myVariable;
Run Code Online (Sandbox Code Playgroud)
不支持将表达式扩展为静态调用/常量的类名/对象(但扩展变量,如上所示).