GitHub Address: Https://github.com/ZQCard/design_pattern
/**/
(1) AbstractCustomer.class.php (abstract parent Class)
<? phpnamespace Nullobject; Abstract class abstractcustomer{ protected$name; Public Abstract function isnil ():bool; Public Abstract function string ;}
(2) RealCustomer.class.php (Real user Class)
<?phpnamespace Nullobject;classRealcustomerextendsabstractcustomer{ Public function__construct (string $name) { $this->name =$name; } Public functionIsnil ():BOOL {return false; } Public functionGetName ():string { return $this-name; }}
(3) NullCustomer.class.php (empty object instead of Class)
<? phpnamespace Nullobject; class extends abstractcustomer{ publicfunctionstring { return "Not Available in Customer Database"; } Public function isnil ():bool { returntrue; }}
(4) CustomerFactory.class.php (User factory Class)
<?phpnamespace Nullobject;classcustomerfactory{ Public Static $users= []; Public Static functionGetCustomer ($name) { if(In_array($name, Self::$users)){ return NewRealcustomer ($name); } return NewNullcustomer (); }}
(5) nullobject.php
<?Phpspl_autoload_register (function($className){ $className=Str_replace(‘\\‘,‘/‘,$className); include $className.". Class.php ";}); Usenullobject\customerfactory; Customerfactory::$users= ["Rob", "Joe", "Julie"];$customer 1= Customerfactory::getcustomer (' Rob ');$customer 2= Customerfactory::getcustomer (' Bob '));$customer 3= Customerfactory::getcustomer (' Joe ');$customer 4= Customerfactory::getcustomer (' Julie ');Echo $customer 1-getName ();Echo' <br/> ';Echo $customer 2-getName ();Echo' <br/> ';Echo $customer 3-getName ();Echo' <br/> ';Echo $customer 4-getName ();Echo' <br/> ';
Empty object mode of design mode (PHP implementation)