[英]Access constant of a class member object doesn't work
if I try the following example (PHP 5.4) I get the following error: 如果我尝试以下示例(PHP 5.4),则会收到以下错误:
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ',' or ';'
解析错误:语法错误,意外的'::'(T_PAAMAYIM_NEKUDOTAYIM),期望为','或';'
class a {
public $p;
public function __construct() {
$this->p = new b;
}
public function giveout() {
echo $this->p::c;
}
}
class b {
const c = '234';
}
$obj = new a;
$obj->giveout();
But why? 但为什么? Isn't it possible to use the double colon and arrow in one expression?
一个表达式中不能使用双冒号和箭头吗? I know I could also use a getter method in class b and then call
$this->p->get()
, but I'd rather like to use the above syntax. 我知道我也可以在类b中使用getter方法,然后调用
$this->p->get()
,但是我想使用上面的语法。
Rewrite 改写
echo $this->p::c;
to 至
echo $this->p=b::c;
and.. 和..
$a->giveout();
to 至
$obj->giveout();
<?php
class a {
public $p;
public function __construct() {
$this->p = new b;
}
public function giveout() {
echo $this->p=b::c;
}
}
class b {
const c = '234';
}
$obj = new a;
$obj->giveout();
OUTPUT : 输出:
234
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.