pytrin wrote:You're basically ignoring that no results were found.
Kind of and kind of not. Like I said I could easily check if the object implements Object_Null and show said message. But consider if you had 20 results you pulled back, and a few of them were just missing odd fields here and there. Typically one would have "N/a" or placeholder photos. Rather than have
if statements everywhere placeholder text could possibly go, a
null object inverts the control, encapsulating that behavior within the model.
how would you tell the difference between:
1. No results found
If its a
plural finder, it would be an
empty array for iteration. $array == array(). If it were a
singular finder it would return a
null object for invoking methods on.
2. Search parameter error
? What do you mean? Like user asked for record 999 but there is no record 999? I'd get a null object and its ID would be 0. So
$model instanceof Object_Null && $model->id() == 0
3. System error
Those cause exceptions
4. Unauthorized error
My model would not be checking permissions. It would return the data, and the controller would decide what to do. Permissions are
system level to me. Normally my controller has a
utility method Like:
This would exit the controller &
dispatch the
error action if need be. Just a simple
assertion.
Further, how could you tell if that result came from the search or is the default value?
If it implements Object_Null, or does not. Or if its id == 0 or not (although the latter could also be an un-persisted model)
Consider you had the following object hooked together
Code: Select all
Person
- Contact
--- Phone
--- Address
--- Email
- Photo
Now lets say the user has not uploaded a photo yet. I would have a regular Person object but it's ->photo() method would not return a regular Photo, it would return a Photo_Null which could either come from
code generation or could be an actual
sub-class I implemented. If you have to use an
if statement everytime your program has
conditional flow, you're doing it wrong IMO. Its much easier to read code that just outputs 100 fields. Its much harder to read the same code after its been peppered with
control structures. Also if youre doing this "list" of users in more than one page, youre duplicating code

(unless you use Null object pattern)
The reason you want to use these null objects is your photo may have a default filename, a default width, a default height, a default alt text, a default href, a default brightness, maybe cropping, maybe it in turn also composes some "Text" objects, etc..
Do you really want all that logic in your views, everytime you need to show a photo? Or would you rather just have a photo object and tell it to ->render() ?