Why model methods return false instead null?

I did not found this discussion, so I will ask: why model methods like find and findFirst return false instead null when no objects are found?

At least for me, this doesn't make any sense, because you have to prepare the application to handle objects and booleans. IMHO, null is the right way to represent the lack of something, or you could write a method like this:

public function someMethod(SomeClass $someObject = false)

Extra shot: rewrite them to return null will bring PHP7's null coalesce operator compatibility.



9.8k

empty(null) and empty(false) produce the same result. I guess I'm not sure what you mean by "prepare the application to handle objects and booleans."

Yeah, you're right. You can handle both types this way, but it doesn't mean it is the best approach, right?

IMHO, null is the right thing to do, with the main selling point being the null coalesce compatibility.

So why don't return it? :-)

edited 25d ago

I agree. I'd be hapier with nulls. In fact, once php 7.2 (i think) stabilishes nullable type hints, it'll make even more sense.

"Null" is far more semantic; it represents an invalid object, which is a far better response for something that we're expecting to return an object, rather than 'false', which sounds like an answer to a yes/no question.