skip to Main Content

I’m trying to share a value of $shippingMethod from the getMainDetails() function to the estimateDeliveryDate() function in the same class to run a conditional but the second function doesn’t appear to be getting the value:

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

Wondered if someone could help?

Thank you.

2

Answers


  1. You need to add your variable as a property, like so:

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

    EDIT

    However, a more solid approach at this would be something along these lines:

    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());
        }
    }
    

    Few of things going on in that example.

    1. I moved shippingMethod() into the Order class as it is not the DeliveryHandlers responsibility so it should not have to care about what’s going on in that method. The data belongs to and comes from Order.

    2. I made getDeliveryDateEstimate() return a string instead of using echo. This makes your code more reusable – eg what if one day you want to pass this to a template or another variable instead of echoing it. This way you keep your options open.

    3. I’m using dependency injection to pass the Order class into the DeliveryHandler, thus making the public interface of Order available to DeliveryHandler.

    If you happen to have a laracast subscription you could check out these courses here, they explain all this stuff in a nice digestible format:

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

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

    Login or Signup to reply.
  2. Use properties of the class ($this->variable), instead of local variables ($variable). You also have a syntax error in your code, where you compare for the value of $shippingMethod.

    The method below assumes that you call getMainDetails() before being able to use estimateDeliveryDate(). You should however ensure that, by having $order being passed to estimateDeliveryDate(), and call getMainDetails() from there (where you return from the getter instead of set a property).

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

    Alternatively, you can return from each function – which is what you should do from methods that is a “getter”, i.e. getMainDetails()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search