传递变量值
我试图从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';
}
}
想知道,如果有人可以帮助?
谢谢。
您需要将您的变量添加为一个属性,像这样:
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());
}
}
事情很少在例如回事。
我感动
shippingMethod()
到Order
类,因为它是不是DeliveryHandler
的责任,因此不应该关心这个方法是怎么回事。该数据属于并来自Order
。我制作的
getDeliveryDateEstimate()
返回一个字符串,而不是使用echo
。这使你的代码的可重用性 - 例如,如果有一天你想将这个传递给一个模板或其他变量,而不是附和它。这样你就可以保持你的选择。我使用依赖注入到
Order
类传递到DeliveryHandler
,从而使得Order
提供的公共接口DeliveryHandler
。
如果你碰巧有一个laracast订阅,您可以在此查看这些课程,他们解释这一切的东西,在一个很好的消化格式:
https://laracasts.com/series/object-oriented-bootcamp-in-php
一个函数被称为'得到...',但不会'返回'任何东西,而是*设置*属性严重错误和/或误导。 – deceze
true @deceze但我没有选择这些名字,我只是复制了作者的代码 – Bananaapple
@deceze设置/获取的函数中有更多的代码,但我不想在这里粘贴所有这些,因为我没有感觉它是相关的。 – doubleplusgood
使用性质类($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()
是的,但是......您如何确保在'estimateDeliveryDate'之前调用'getMainDetails',即状态是一致的? – deceze
的最佳方式可能需要'getMainDetails(...)'返回'$ shippingMethod',然后将其作为参数传递给'estimateDeliveryDate(...)' – JCOC611
http://php.net/manual/de/langua ge.variables.scope.php在那里你会找到你需要的一切。让我知道你是否需要更多的帮助 – floGalen
问题是你的'estimateDeliverDate'方法没有定义'$ shippingMethod'变量。如果将它保存到对象中,其他方法可以使用它。 '$ this-> shippingMethod = strotolower(...);',然后在你的其他函数中执行'$ this-> shippingMethod =='星期六发货'' – BizzyBob