We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.


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 Jul '17

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.

edited Jan '19

+1 for null.

For example this wont work:

  • //Some where:
  • $row = Model::findFirst($id);
  • MyClass::add($row);

  • //Method signature:
  • public static function add(?Model $row)