2016-08-19 53 views
2

我想弄清楚爲什麼控制器類無法訪問它擴展的父級的屬性。無法從子對象訪問父變量

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

     } 
    } 
} 
+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'; 
    
         } 
        } 
    } 
    

此外,注意:你並不需要實例雙方父母和孩子,當你在第幾行所做的:

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

您應該只實例化的孩子,通過孩子的構造函數發送數據,一旦孩子的構造已經匹配了父母的允許數據流(見我上面如何工作提供的代碼):

$controller = new OrderLinkController($data);