コントローラクラスが拡張する親のプロパティにアクセスできない理由を理解しようとしています。子オブジェクトから親変数にアクセスできない
$ thisを使用して配送方法を取得すると、何も出力されません。 var_dumpは、それが「未定義クラス定数 『shipinfo』」
私が間違っているのかについての任意のアイデアを、エラーを返す::親を使用して0
の文字列の長さを持つ配列であると言いますか?親クラスが拡張されたときにpublic/protected変数にアクセスできると思いましたか?
$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