2017-09-01 119 views
-1

我試圖從getMainDetails()功能共享的$shippingMethod一個值estimateDeliveryDate()功能在同級別運行條件,但第二個功能似乎並沒有被獲取值:傳遞變量值

private function getMainDetails($order) 
{ 
    //This value is correctly being set from my order details in Magento 
    $shippingMethod = strtolower($order->getShippingDescription()); 
} 

private function estimateDeliveryDate($orderCreatedDate) 
{ 
    if ($shippingMethod == 'saturday delivery') { 
     echo 'Saturday Delivery'; 
    } else { 
     echo 'Standard Delivery'; 
    } 
} 

想知道,如果有人可以幫助?

謝謝。

+1

的最佳方式可能需要'getMainDetails(...)'返回'$ shippingMethod',然後將其作爲參數傳遞給'estimateDeliveryDate(...)' – JCOC611

+0

http://php.net/manual/de/langua ge.variables.scope.php在那裏你會找到你需要的一切。讓我知道你是否需要更多的幫助 – floGalen

+0

問題是你的'estimateDeliverDate'方法沒有定義'$ shippingMethod'變量。如果將它保存到對象中,其他方法可以使用它。 '$ this-> shippingMethod = strotolower(...);',然後在你的其他函數中執行'$ this-> shippingMethod =='星期六發貨'' – BizzyBob

回答

1

您需要將您的變量添加爲一個屬性,像這樣:

class myClass 
{ 
    private $shippingMethod; 

    private function getMainDetails($order) 
    { 
     //This value is correctly being set from my order details in Magento 
     $this->shippingMethod = strtolower($order->getShippingDescription()); 
    } 


    private function estimateDeliveryDate($orderCreatedDate) 
    { 
     if ($this->shippingMethod == 'saturday delivery') 
     { 
      echo 'Saturday Delivery'; 
     } else { 
      echo 'Standard Delivery'; 
     } 

    } 
} 

編輯

然而,在這個更加堅實的做法是沿着這些路線的東西:

class DeliveryHandler 
{ 
    private $order; 

    public function __construct(Order $order) 
    { 
     $this->order = $order; 
    } 

    private function getDeliveryDateEstimate() 
    { 
     if ($this->order->getShippingMethod() == 'saturday delivery') { 
      return 'Saturday Delivery'; 
     } else { 
      return 'Standard Delivery'; 
     } 

    } 
} 

class Order 
{ 
    public function getShippingMethod() 
    { 
     return strtolower($this->getShippingDescription()); 
    } 
} 

事情很少在例如回事。

  1. 我感動shippingMethod()Order類,因爲它是不是DeliveryHandler的責任,因此不應該關心這個方法是怎麼回事。該數據屬於並來自Order

  2. 我製作的getDeliveryDateEstimate()返回一個字符串,而不是使用echo。這使你的代碼的可重用性 - 例如,如果有一天你想將這個傳遞給一個模板或其他變量,而不是附和它。這樣你就可以保持你的選擇。

  3. 我使用依賴注入到Order類傳遞到DeliveryHandler,從而使得Order提供的公共接口DeliveryHandler

如果你碰巧有一個laracast訂閱,您可以在此查看這些課程,他們解釋這一切的東西,在一個很好的消化格式:

https://laracasts.com/series/object-oriented-bootcamp-in-php

https://laracasts.com/series/solid-principles-in-php

+0

一個函數被稱爲'得到...',但不會'返回'任何東西,而是*設置*屬性嚴重錯誤和/或誤導。 – deceze

+0

true @deceze但我沒有選擇這些名字,我只是複製了作者的代碼 – Bananaapple

+1

@deceze設置/獲取的函數中有更多的代碼,但我不想在這裏粘貼所有這些,因爲我沒有感覺它是相關的。 – doubleplusgood

2

使用性質類($this->variable),而不是局部變量($variable)的。您還可以在你的代碼,在這裏就可以比較的$shippingMethod值有語法錯誤。

下面的方法假定你叫getMainDetails()之前能夠使用estimateDeliveryDate()。然而,你應該確保,具有$order傳遞給estimateDeliveryDate(),並從那裏調用getMainDetails()(您return從吸氣劑,而不是設置屬性的位置)。

class Foo (
    private $this->shippingMethod; // Initialize 

    /* Your other methods */ 

    private function getMainDetails($order) 
    { 
     //This value is correctly being set from my order details in Magento 
     $this->shippingMethod = strtolower($order->getShippingDescription()); 
    } 

    private function estimateDeliveryDate($orderCreatedDate) 
    { 
     if ($this->shippingMethod == 'saturday delivery') { 
      echo 'Saturday Delivery'; 
     } else { 
      echo 'Standard Delivery'; 
     } 
    } 
) 

或者,您可以return從每一個功能 - 這是你應該從作爲 「吸氣劑」 的方法做什麼,即getMainDetails()

+1

是的,但是......您如何確保在'estimateDeliveryDate'之前調用'getMainDetails',即狀態是一致的? – deceze