将对象分配为 属性 [php] 后无法使用 static 关键字
Lose ability to use static keyword after assigning object as property [php]
我不明白为什么下面输出变量:
class Sample(){
public $query;
function __construct() {
$test = new \SolrQuery();
echo $test::FACET_SORT_INDEX;
exit();
}
}
但这给了我一个致命错误:
[Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM,
expecting ',' or ';']
class Sample(){
public $query;
function __construct() {
$this->query = new \SolrQuery();
echo $this->query::FACET_SORT_INDEX;
}
}
我猜这与变量的权限有关?我能做些什么来解决这个问题吗?
尝试\SolrQuery::FACET_SORT_INDEX;
这是来自 class 而不是对象
的常量
鉴于此:
class foo {
static public $foo = 'foo!';
}
class bar {
public $query;
function t1() {
$test = new foo();
echo $test::foo;
}
function t2() {
$this->query = new foo();
echo $this->query::foo;
}
function t3() {
echo foo::$foo;
}
}
$x = new bar();
$x->t1(); /// dies with "undefined class constant 'foo'
$x->t2(); /// dies with unexpected T_PAAMAYIM_NEKUDOTAYIM
$x->t3(); // works, prints "foo!"
基本上,您正在尝试使用错误的语法访问静态 class 属性。
我不明白为什么下面输出变量:
class Sample(){
public $query;
function __construct() {
$test = new \SolrQuery();
echo $test::FACET_SORT_INDEX;
exit();
}
}
但这给了我一个致命错误:
[Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';']
class Sample(){
public $query;
function __construct() {
$this->query = new \SolrQuery();
echo $this->query::FACET_SORT_INDEX;
}
}
我猜这与变量的权限有关?我能做些什么来解决这个问题吗?
尝试\SolrQuery::FACET_SORT_INDEX;
这是来自 class 而不是对象
鉴于此:
class foo {
static public $foo = 'foo!';
}
class bar {
public $query;
function t1() {
$test = new foo();
echo $test::foo;
}
function t2() {
$this->query = new foo();
echo $this->query::foo;
}
function t3() {
echo foo::$foo;
}
}
$x = new bar();
$x->t1(); /// dies with "undefined class constant 'foo'
$x->t2(); /// dies with unexpected T_PAAMAYIM_NEKUDOTAYIM
$x->t3(); // works, prints "foo!"
基本上,您正在尝试使用错误的语法访问静态 class 属性。