我想弄清楚爲什麼控制器類無法訪問它擴展的父級的屬性。無法從子對象訪問父變量
使用$ this檢索shipping方法不會輸出任何內容。的var_dump說,這是與0
字符串長度使用::返回錯誤「未定義類常量‘shipinfo’」
什麼我做錯了任何想法父數組?我認爲當父類被擴展時,可以訪問公共/受保護的變量嗎?
$data = trim(file_get_contents('php://input'));
$link = new OrderLink($data);
$controller = new OrderLinkController();
class OrderLink{
protected $shipinfo = [
'name' => '',
'address' => '',
'unit' => '',
'city' => '',
'state' => '',
'country' => '',
'zip' => '',
'phone' => '',
'email' => '',
'method' => ''
];
protected $items;
function __construct($postdata)
{
$xml = simplexml_load_string($postdata);
$xml = $xml->Order;
$billinfo = $xml->AddressInfo[1];
$this->shipinfo['name'] = strval($billinfo->Name->Full);
$this->shipinfo['address'] = strval($billinfo->Address1);
$this->shipinfo['unit'] = strval($billinfo->Address2);
$this->shipinfo['city'] = strval($billinfo->City);
$this->shipinfo['state'] = strval($billinfo->State);
$this->shipinfo['country'] = strval($billinfo->Country);
$this->shipinfo['zip'] = strval($billinfo->Zip);
}
}
class OrderLinkController extends OrderLink
{
function __construct(){
echo 'Shipping Method: ' . $this->shipinfo['method'];
echo parent::shipinfo['method'];
if ($this->shipinfo['method'] == 'Local Pickup'){
$this->shipinfo['method'] = 'Pickup';
}
}
}
您正在覆蓋子類中的父類__construct()方法。 –
因此,如果我從孩子中刪除構造函數,變量將被訪問? – Query
@Query - 不,你不需要移除構造函數,請看下面的答案,我已經添加了一個註釋來詳細解釋這個概念 – Katie