2016-08-19 6 views
2

コントローラクラスが拡張する親のプロパティにアクセスできない理由を理解しようとしています。子オブジェクトから親変数にアクセスできない

$ 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'; 

     } 
    } 
} 
+0

子クラスの親クラス__construct()メソッドを上書きしています。 –

+0

したがって、子からコンストラクタを削除すると、変数にアクセスできますか? – Query

+0

@Query - いいえ、あなたはコンストラクタを削除する必要はありません、以下の私の答えを見てください、私は詳細をこの概念を説明するためにさらに注記を追加しました – Katie

答えて

1

私も、ケイティの答えに、コード自体に問題の横にそれを追加することになり、クラス構造にも問題があります。

あなたが持っているクラスは、Webアプリケーションにとって典型的なものです。OrderLinkControllerは、着信要求を処理するクラスです。入力データを取得し、検証し、ビジネスロジックオブジェクトのModelに渡します。

この場合、OrderLinkはモデルであり、コントローラとの間に親子関係は存在してはいけません。これらは独立したままにしておく必要があります。

// it may extend some BaseController class, but not the model class 
class OrderLinkController 
{ 

    function run() { 
     // get and parse the input data 
     $data = trim(file_get_contents('php://input')); 
     $xml = simplexml_load_string($data); 
     $xml = $xml->Order; 
     // validate the data 
     if ($xml->method == 'Local Pickup'){ 
      $xml->method = 'Pickup'; 
     } 
     // pass the data to the model 
     $model = new OrderLink($xml); 
     // save the data or do something else with it 
     $model->save(); 

     echo $model->getShippingInfo(); 
    } 
} 


class OrderLink{ 

    // it is usually better to have explicitly defined fields 
    // rather than an array 
    protected $name; 
    protected $address; 
    protected $unit; 
    protected $city; 
    protected $state; 
    protected $country; 
    protected $zip; 
    protected $phone; 
    protected $email; 
    protected $items; 

    // model gets already parsed data 
    function __construct($data) 
    { 
     $billinfo = $data->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); 
    } 

    public function getShippingInfo() { 
     return 'Shipping Method: ' . $this->method; 
    } 
} 

// create and run the controller 
$controller = new OrderLinkController(); 
$controller->run(); 
4

小さな問題のカップル:あなたは親のコンストラクタを呼び出す必要があるので、あなたが子供を介して親に必要な$のPOSTDATAを流し(または子のコンストラクタ内に作成する)必要があります

  • あなたがshipinfo

    class OrderLinkController extends OrderLink 
    { 
    
        function __construct($postdata){ 
    
         parent::__construct($postdata); 
    
         echo 'Shipping Method: ' . $this->shipinfo['method']; 
         echo $this->shipinfo['method']; 
    
         if ($this->shipinfo['method'] == 'Local Pickup'){ 
          $this->shipinfo['method'] = 'Pickup'; 
    
         } 
        } 
    } 
    
のための$ this - >を使用することができます

さらにノート:あなたが唯一の子をインスタンス化する必要があり

$link = new OrderLink($data); 
$controller = new OrderLinkController(); 

一度、子供のコンストラクタを介してデータを送信し、:あなたが最初の数行で行ったようにあなたは、親と子の両方をインスタンス化する必要はありません。子供のコンストラクタは(私はこれがどのように機能するかについては、上記提供されたコードを参照)のデータが流れるようにするために、親のができるようにマッチしています

$controller = new OrderLinkController($data); 
関連する問題