簡體   English   中英

類成員對象的訪問常量不起作用

[英]Access constant of a class member object doesn't work

如果我嘗試以下示例(PHP 5.4),則會收到以下錯誤:

解析錯誤:語法錯誤,意外的'::'(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();

但為什么? 一個表達式中不能使用雙冒號和箭頭嗎? 我知道我也可以在類b中使用getter方法,然后調用$this->p->get() ,但是我想使用上面的語法。

改寫

echo $this->p::c;

echo $this->p=b::c;

和..

$a->giveout();

$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();

輸出:

234

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM