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?
$person = new person();would be as effective as$person = new person(NULL, NULL, NULL);__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.PersonUnknown;)