PHP implements null object mode

Source: Internet
Author: User

Null object pattern (NULL, type): Replaces null with an empty object, reducing the check of the instance. Such an empty object can provide default behavior when the data is unavailable

(a) Why empty object mode is required
解决在需要一个对象时返回一个null值,使其调用函数出错的情况
(ii) Empty object mode UML diagram

Java is the empty object mode UML diagram, many PHP design patterns on the Web code implementation is based on the above UML diagram

In fact, PHP is simpler to implement in empty object mode than Java. Because PHP has wonderful syntax for sugar, the Magic method __call method.

We can implement the empty object schema as long as we implement the __call method of the empty object, and we do not need to use Nullobject to inherit the corresponding abstract object.

(c) Simple examples

Let's say we have this piece of code now.

<?php//test class < Span class= "Hljs-keyword" >class person{public function code () { Span class= "Hljs-keyword" >echo  ' Code makes me happy '. Php_eol; }}//defines a Build object function, only phper allows the generation of objects function getperson ($name) {if ($name == ' Phper ') {return new person; }} $phper = Getperson ( ' Phper '); $phper->code ();        

Yes, the code makes me happy is now output. If sometimes this function is called by someone else, it does not pass in the appropriate parameters?

$phper = getPerson(‘Javaer‘);$phper->code();

It's going to be an error at this point. Error:call to a member function code () is on null. Yes, $phper is now a null value, so calling the code method will cause an error.

This is a common situation where the system does not return an object that we expect, but instead returns a null value. So in most cases, our code is written like this.

$phper = getPerson(‘Javaer‘);if(!is_null($phper)){    $phper->code();}

or a

if(is_object($phper)){    $phper->code();}

This allows too many if judgments to inevitably exist in our code
If we use Nullobject mode, we can let the function return a Nullobject object without returning a value. Instead of a null value (no return default null value)

Test classClassperson{Public function code () {echo  ' Code makes me happy '. Php_eol; }}//empty object mode class nullobject{}//defines a Build object function that only phper allows to generate objects function getPerson ($name) {if ($name == ' Phper ') {return new person;} else{return new NullObject;} $phper = Getperson (       

At this point, you will not be able to report a null calling function error, but will report a call to undefined method error, because the Nullobject object does not have the code method. This time we only need to implement the Magic method __call, will not error.

//空对象模式class NullObject{    public function __call($method,$arg){ echo ‘this is NullObject‘; }}

We can replace the return null by returning a Nullobject object, so that we don't have to judge if the method is null, and as long as you implement the call method, regardless of the actual object It was originally called that method, Nullobject can go to call and no error ( 实际是隐式调用了魔术方法call). Of course, if your original logic is that the return object is null and nothing is done, then you can let __call () do nothing. Or you can let it throw an exception.

PHP implements null object mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.