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.
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.
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.