2

I have a class to manipulate orders. I have created multiple methods for each purpose too. There can be multiple orders to process which is generated from db. Right now, what I am doing is that, loop through each order and create objects with order id as param to constructor.

foreach($order_row as $order_rows)
{
       $order_id=$order_rows->order_id ;
       $warehouse =new WarehouseManager($order_id);
       $warehouse->ProcessWarehouse();
}

Is it okay to loop like this? Is there any better way to handle this?

4
  • This looks like it'll work fine. Do you foresee any specific problems with it? The "best solution" generally depends more on context and goal than anything else. Commented Dec 26, 2014 at 9:25
  • Create array of objects, so that you can even access the all orders after the loop. Commented Dec 26, 2014 at 9:25
  • You're overwriting $warehouse on each iteration, and it doesn't really look like you would need a new instance of the WarehouseManager for every iteration, but that really depends on what the class does ? Commented Dec 26, 2014 at 9:29
  • It is important to send the order id through constructor. So this will do good? Commented Dec 26, 2014 at 10:50

1 Answer 1

2

You don't need to create new object for each order. What if there is a huge number of records returned?, You only need to create one object to process an order one by one.

$warehouse = new WarehouseManager();
foreach($order_row as $order_rows)
{
    $order_id=$order_rows->order_id ;
    $warehouse->setOrder($order_id);  // this method should be implemented first
    $warehouse->ProcessWarehouse();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.