4

I have this:

$itemCode = $area->pickSpecificItemByType($type)->getItemCode();

It's possible that $area->pickSpecificItemByType($type) might return null. Thus calling getItemCode() would be impossible.

What is the cleanest and/or most efficient way to do this?

Is there any better option than this?

$itemCode = (!is_null($area->pickSpecificItemByType($type))) ?
     $area->pickSpecificItemByType($type)->getItemCode() : '';
b85411
  • 9,420
  • 15
  • 65
  • 119
  • I don't think there is another way – Said Kholov May 18 '15 at 03:24
  • Could setup a `try catch` and throw an error when `pickSpecificItemByType` returns null. That way it wont progress to the next method. Haven't tested it but in theory it sounds like it should work – Bankzilla May 18 '15 at 05:08

4 Answers4

7

Rather than call pickSpecificItemByType() twice, you could do:

$item = $area->pickSpecificItemByType($type);
$itemCode = ($item) ? $item->getItemCode() : '';

Although another variable is introduced ($item) you gain in terms of readability.

Richard Snazell
  • 242
  • 1
  • 8
  • Instead of just checking if `$item` is returning something evaluating to boolean `true`, it might be better to check `is_object($item) && method_exists($item, 'getItemCode')` to make it completely foolproof. – Ulver May 18 '15 at 03:59
2

--- [ edit ] ---

The original reply below was written for PHP 5.*.

For modern PHP (7, 8), please see the null-coalescing operator ?? and (in PHP8) the null-safe ?-> operator

https://stackoverflow.com/a/63222028/773522

---[ original ]---

$itemCode = @$area->pickSpecificItemByType($type)->getItemCode();

calling getItemCode() would not be impossible, it would just issue a Error.

the '@' control character tells PHP to ignore the warning. If you get into this warning state, you'll get an empty string (which is what your ternary code was using), so it's a pretty even replacement.

http://php.net/manual/en/language.operators.errorcontrol.php

fbas
  • 1,676
  • 3
  • 16
  • 26
  • 1
    This seems like a horrible idea, you do not want to suppress error messages because you want to have your "pretty" syntax. – Stephan-v Oct 25 '18 at 07:37
  • 1
    what is horrible about this is not that it will suppress `the call on null` error, (you're ok with that now), but it will suppress any other error on this line as well and it will die with blank screen of death without any clue for your future-self and you will debug for hours till you uncover the much needed error message consumed by this one unfortunate '@' - trust me, been there – ptica Mar 10 '21 at 08:38
  • Theres nothing horrible about this idea. The code in question calls a getter method. Its absolutely safe to say that the only error that can ever be thrown here is the object being null. – j4k3 Mar 13 '21 at 08:12
  • @j4k3 that's not true. – Daniel W. Apr 07 '21 at 15:26
  • @j4k3 you cannot guarantee that. Sometimes getters can do other things too. Also it can be e.g. wrong type (if the method declares it). – Alex P. Oct 28 '21 at 11:53
-1

Would something like this be better?

if( empty( $itemCode ) )
{
// $itemCode is empty.
}else{
// Do Something
}
Damo85
  • 65
  • 8
-1

You can use the below code.

$item = pickSpecificItemByType($type);

if( $item == '' )
    {
    // if $itemCode is empty.
    }else{
    // Do the stuff that you wanted to do if $item is not null
     $area->$item->getItemCode();
    }

I hope this helps you.

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33