PHP - How to handle method calls

时间:2015-06-25 18:32:39

标签: php

Which is the better way to handle the following case? I've been wondering about this for a while and figured I'd ask.

Define a variable?

$getItems = $itemTools->getItems($item_id);

if($getItems)
{
  //do stuff
}

Or

if($itemTools->getItems($item_id))
{
  //do stuff
}

1 个答案:

答案 0 :(得分:0)

IMHO it would depend on what you were doing after/within the if branch. If your plan is doing something with the returned data then it makes sense to me storing the results in a var. Otherwise it doesn't. Looking at the function name I'm assuming it is not a simple check but a function returning 'usable' data so probably you will use this data and you actually need the var. Unless you are doing this in the global scope, if you are doing this within some method then php will release the memory used by this var once the method ends. What about doing another (and quick) method named 'hasResults($itemId)' returning a boolean? Just wondering