1

I just read a great article about the Null Object Pattern (http://phpmaster.com/the-null-object-pattern-polymorphism-in-domain-models/) and I'm planning on implementing it into some existing code.

my question is would a whole new 'null class' be the best way about doing this? Previously I had set default values in the constructor as NULL. Then I could just create empty shells of the class. For example:

class person{

    private $_personId;
    private $_name;
    private $_email;
    private $_phone;

    public function __construct($_personId, $_name, $_email, $_phone = NULL){
        //set the vars here
    }

}

Then if I wanted a real object I would do:

$person = new person(1, 'John Doe', '[email protected]');

And if I wanted a 'null' object i would do:

$person = new person(NULL, NULL, NULL);

Are there any pitfalls to this approach?

5
  • 4
    As you've provided NULL defaults for all arguments, $person = new person(); would be as effective as $person = new person(NULL, NULL, NULL); Commented Mar 13, 2013 at 22:57
  • you're right, i screwed that question up. i'll edit it. Commented Mar 13, 2013 at 23:04
  • 1
    If you don't do any real checking, and Nulls are allowed, there's not much reason for a NullObject, is there? With __construct($_personId = NULL, $_name = NULL, $_email = NULL), you are stating that all arguments are optional. If they are NOT, you shouldn't give them default NULL values, but just state __construct($_personId, $_name, $_email) (or the first may be required, the 2 after that optional, etc.). Which will generate errors if the arguments are not passed. Juicy, useful, to-the-point errors in your log which let you debug oh so quickly. Commented Mar 13, 2013 at 23:05
  • It's not obvious: is your person null object or a user that hasn't filled in the name and email, and isn't created yet? That's why I think it's better to create a different class, like PersonUnknown ;) Commented Mar 13, 2013 at 23:05
  • sorry to the people who just commented, i screwed up the question. it's edited now Commented Mar 13, 2013 at 23:06

2 Answers 2

4

You are not following the discussed pattern in the article. The principles of Null Object is extend your main Domain object. Would be something like this:

class Person {
    // atributes and methods...
}

class PersonNull extends Person {

}
Sign up to request clarification or add additional context in comments.

Comments

1

I encourage you to consider exploring the Factory pattern - that is, creating several static methods inside Person that can return a Person object based upon the passed parameters. For example:

class Person {
 public static createWithName($name) {
   $obj = new Person();
   $obj->_name = $name;
   return $obj;
 }

 public static createWithNameAndEmail($name, $email) {
   $obj = new Person();
   $obj->_email = $name;
   $obj->_name = $email;
   return $obj;    
 }

 ....
}

/* 
   And then instead of $objPerson = new Person('Bob', null, null, null)
   you would instantiate an object like this:
*/
$objPerson = Person::createWithName('Bob')

This will allow you to do all the verifications you need, and help you document the valid kind of parameters through the method declarations. More on the factory pattern: http://phpmaster.com/understanding-the-factory-method-design-pattern/

4 Comments

two things. i'm no php guru, but from my understanding of the factory pattern, it isn't what you're describing above. doesn't the factory have to do with dynamic class names, not dynamic method names? Second, I don't think this is addressing the original problem. I need to make a completely empty object, no name, no nothing. are u suggesting having a method called create null object?
I would shy away from static methods. Try to test. Dependency injection is usually the better way to go.
@CommandZ Factory pattern is probably the only place in the code where static methods are justified. Those two methods presented in an answer are just simple shorthands for initialization of the domain model in question.
Old post, but static methods are proper use for many things. Consider the Singleton pattern. Try not to hinder one's self by denying yourself tools that are at your disposal. There are even reasons to violate "good" programming practice at times.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.