Null object pattern

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
piccoloprincipe
Forum Newbie
Posts: 12
Joined: Wed Jan 16, 2008 4:11 pm

Null object pattern

Post by piccoloprincipe »

I have implemented the null object pattern for some of my classes, but it requires one interface extraction and one class (for the null object) for every class in my environment.
Is there a faster way to construct null objects?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Null object pattern

Post by Christopher »

You could create an implementation of the Null Object pattern in PHP by just doing:

Code: Select all

class NullObject {
    public function __call($name, $params) {}
}
That is actually a pretty nice idea. Take that Ruby, Python and Java!!!! ;) :)
(#10850)
piccoloprincipe
Forum Newbie
Posts: 12
Joined: Wed Jan 16, 2008 4:11 pm

Re: Null object pattern

Post by piccoloprincipe »

That's useful :|
But I was thinking that's a reason for NullObject having to implement the specific interface of the relative class: consistency of the methods. Think of a method hasNotPermission() that have to always return TRUE in case of the null object: I have come to the conclusion that null object should have a specific behavior for every class that it's linked to, and clean code have to specify more and more null objects.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Null object pattern

Post by Christopher »

How about:

Code: Select all

class NullObject {
    protected $methods = array();
    public function __call($name, $params) {
        return isset($this->methods[$name]) ? $this->methods[$name] : null;
    }
}
 
class MyClass extends NullObject {
    protected $methods = array('hasNotPermission' => true);
}
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Null object pattern

Post by Chris Corbyn »

Scary 8O

EDIT | Sucks for type-hinting though.

Is there a valid place this would be used? I could see it being potentially useful in testing but using it in production would scare the hell outta me :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Null object pattern

Post by Christopher »

I get the impression that you would provide one of these object at initialization so the object using it would not need to do null pointer checks. It would not error and would return "nothing happened" type results. Then (I guess ;)) you would provide a fully functional object as a replacement at some point. Maybe for things like callback architectures?
(#10850)
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: Null object pattern

Post by sike »

i use the concept of "null" objects quite alot. it helps making my code easier to read and shortens it quite a bit.

a simple example is iterating over a collection of items :
i always return a iterator regardles of the fact that there is something to iterate. if there is nothing to iterate a NullIterator is returned which a) signals that there is nothing to iterate and b) behaves exactly like a normal iterator. that pays off in terms of not having to check if the returned iterator is valid.

Code: Select all

 
  var it:Iterator = foo.getIterator();
  if (it)
  {
      while(it.hasNext())
      {
       }
   }
 
vs

Code: Select all

 
  var it:Iterator = foo.getIterator();
  while(it.hasNext())
  {
  }
 
there are many places where this concept is useful but fowler is better at explaining this kind of things : http://books.google.com/books?id=1MsETF ... LtIALWUNcw

cheers
Chris
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Null object pattern

Post by Benjamin »

My question would be: Is the time spent creating and maintaining this extra code less then the time spent adding a few characters to the while loop? I just don't see it. Seems bloatish to me.

Code: Select all

 
 while(it.hasNext())
 
vs

Code: Select all

 
 while(it && it.hasNext())
 
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: Null object pattern

Post by sike »

thats the problem with simple examples (;
my main use for nullobjects is reducing tedious error checking on returned values. granted "it && .." does the job on that particular example well enough but thinking of more complex things it get smore and more useful (at least for me)

chris
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Null object pattern

Post by Benjamin »

sike wrote:thats the problem with simple examples (;
my main use for nullobjects is reducing tedious error checking on returned values. granted "it && .." does the job on that particular example well enough but thinking of more complex things it get smore and more useful (at least for me)

chris
I know what you mean, I've got some pretty wacked out code as well. But in the end it does some incredible things. The closest thing I have ever used to this is an empty function that does nothing except return true. I can't remember offhand what I used that for though.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Null object pattern

Post by Ollie Saunders »

That is actually a pretty nice idea. Take that Ruby, Python and Java!!!!
*Cough* I think you'll find Ruby does it just as easily...

Code: Select all

class NilResponder
    def method_missing(name, args)
    end
end
...possibly better because you would need to define __get() and __set() in PHP to match what method_missing does.

This is a documented refactor. I've used it before in PHP but it's a bit of a hassle because, as mentioned, of type hinting. It works much better in Ruby where you don't type hint.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Null object pattern

Post by Chris Corbyn »

__get() and __set() handle property access. Does method_missing() do a combination of faux method invokation and property access? :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Null object pattern

Post by Ollie Saunders »

Does method_missing() do a combination of faux method invokation and property access?
Considering you already use methods to get access to data in PHP there's really nothing fauxy about it using methods to access data. As such Ruby doesn't have public properties at all. Everything is a method. But methods are not named beginning with 'set' and 'get' so the distinction between data and behaviour is based entirely on the name of the method which is perfectly sufficient.
Post Reply