Yii Framework Forum: Null Object pattern implementation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Null Object pattern implementation Rate Topic: -----

#1 User is offline   hijarian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 28-January 14

Posted 28 February 2017 - 06:03 AM

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 ActiveRecord 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 `$order->item->brand->name` in case of broken link to `brand`. 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 `<span class="not-set">(не задано)</span>` hardcode from GridView columns.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,992
  • Joined: 17-January 09
  • Location:Russia

Posted 28 February 2017 - 08:37 AM

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.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#3 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,992
  • Joined: 17-January 09
  • Location:Russia

Posted 28 February 2017 - 08:37 AM

It may come in handy for grids though...
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#4 User is offline   hijarian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 28-January 14

Posted 28 February 2017 - 02:02 PM

View Postsamdark, on 28 February 2017 - 08:37 AM, said:

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.

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

View Postsamdark, on 28 February 2017 - 08:37 AM, said:

It may come in handy for grids though...


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

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,992
  • Joined: 17-January 09
  • Location:Russia

Posted 28 February 2017 - 03:58 PM

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.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#6 User is offline   hijarian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 28-January 14

Posted 01 March 2017 - 06:35 AM

View Postsamdark, on 28 February 2017 - 03:58 PM, said:

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

#7 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,469
  • Joined: 10-October 10
  • Location:Denmark

Posted 01 March 2017 - 06:59 AM

It's better than adding rarely used features to a framework, IMO. :)
"Less noise - more signal"
0

#8 User is offline   hijarian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 28-January 14

Posted 01 March 2017 - 11:00 AM

View Postjacmoe, on 01 March 2017 - 06:59 AM, said:

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

#9 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,992
  • Joined: 17-January 09
  • Location:Russia

Posted 04 March 2017 - 03:39 AM

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.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users