Returning NULL when the object is unavaliable increase the complexity of function. The function can return an empty object or empty array instead of NULL.
But NULL is still useful to represent the third state “unknown” other than “yes” and “no”
Why NULL references are a bad idea
1 | class BurgerRepository { |
If NULL reference is used user, need to check the return value everytime1
2
3
4$burger = $burgerRepository->get(10);
if($burger) {
$customer->eat($burger);
}
If you don’t mind the logic flow is broke, we can throw error directly. Burger can not be null, but there a “no burger” error make sense.
1 | class BurgerRepository { |
For some case, returning an empty object / array is also a good choice
1 | class BurgerRepository { |