Having read of the benefits of sometimes using NullObjects here, I suddenly spotted a need for one.
Say I have a simple CRUD app with 2 data entry forms.
-edit an existing record
-add a new record
and, as usual I'd like to use the same form for both purposes, here is one field:
It dawns on me that a null page class could do just fine for the latter case without having to do some nasty forkingPHP Code:// on the edit form
<input type='text' id='title' name='title' value='{$page->title}' />
// on the add new form
<input type='text' id='title' name='title' value='' />
if edit is truePHP Code:class NullPage{
public $title = NULL ;
}
$page = (get a page result set using the object notation)
else
$page = new NullPage ;
<input type='text' id='title' name='title' value='{$page->title}' />
Brilliant.
I suppose there is another way of course, just set the vars in my code
$page->title = '' ;
But say I then move off to work on People, then I'd need to make another, the NullPeople Class.
Right, so whats the best way I can make a generic NullObject that always returns null values no matter what properties I call?
(some __call magic no doubt ... I will be looking at that now)
But I have this nagging doubt, is there no other magical way of doing the same thing without actually having to (include and then) create a class at all?







Bookmarks