Null Object pattern implementation

I just wanted to ask, whether there were any plans to incorporate the Null Object pattern implementation in Yii 2 among the core developers or contributors.

For the curious, rationale is that it would be really helpful when added to the concept of relations. With built-in support for Null Object creation for [font="Lucida Console"]ActiveRecord [/font]models we can have transparent empty values when using relation chains.

I mean, no more "trying to get property of null" PHP Fatal when doing chain get like [font="Lucida Console"]$order->item->brand->name[/font] in case of broken link to [font="Lucida Console"]brand[/font]. Just configured in advance appropriate empty value. Or even full Special Case automatic creation, with method overrides and such.

I’m asking because I’m thinking about contributing but don’t want another NIH piece of code. This can be made in relatively non-invasive way by adding traits. Also, it’ll refactor out the [font="Lucida Console"]<span class="not-set">(не задано)</span>[/font] hardcode from [font=“Lucida Console”]GridView [/font]columns.

I’ve thought about that and usually having a null-object for AR isn’t what’s needed. In many cases you want a single “No comment” (or other object name) instead of bunch of empty fields.

It may come in handy for grids though…

Got it. So, there were no internal discussion on that topic, right?

Grids were the initial itch that I had an urge to scratch with this feature.

There were multiple discussions among team members. We agreed that it could be useful in some cases but you can’t apply it to everything.

For grids, I think, you can make a custom column that does all the checks needed before accessing a property. Could work better than null object.

There’s nothing in software engineering which cannot be solved by adding another abstraction layer, huh? :)

Anyway, thanks for clearing that out. You helped me a lot.

It’s better than adding rarely used features to a framework, IMO. :)

While I totally agree that rarely used features should not belong to core framework, I personally would not consider Null Object “rarely used feature”. It is rarely used as of now because people don’t understand its benefits and it’s generally simpler to do return null all the time. If there’ll be no easy support for it there’ll be no usage of it.

hijarian, if you have a good idea on how to implement it so it’s useful in general, I’d like to have your thoughts at GitHub.

Hello. I’m new in Yii2 and I can suggest stupidity, but still I’ll take a chance.

I just wondered exactly the same question when I got tired of writing 100 times something like that:

$Car = Car::findOne([‘id’ => $car_id]);

if (!$Car) {

$Car = new Car();


And he wondered why they did not implement the Nill Object pattern.

To make the code look simpler:

$Car = new Car($car_id);

As a result, I created __construct () for model:

Class Car extends ActiveRecord {

protected $ _id_field_name = ‘id’;

public function __construct ($id = null) {

If (!(int)$id)



$Obj = Car::findOne([$this->_ id_field_name => (int)$id]);

if ($obj) {

  $this->setAttributes($obj->getAttributes(), false);





Perhaps this is contrary to some global ideas Yii2 Framework.

Correct or tell me what can be the problem with this approach.

Thank you.

The reason for AR models case is simple. There’s a huge difference between “not found” and “existing or new”. These cases should be handled differently.