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: 5,184
  • 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 can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • 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 can work more on Yii: 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: 5,184
  • 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 can work more on Yii: 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,694
  • 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.
1

#9 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • 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 can work more on Yii: https://www.patreon.com/samdark
0

#10 User is offline   Dan82 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-June 17

Posted 26 June 2017 - 12:09 AM

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)
      return;
      
    $Obj = Car::findOne([$this->_ id_field_name => (int)$id]);
    if ($obj) {
      $this->setAttributes($obj->getAttributes(), false);
      $this->setIsNewRecord(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.
0

#11 User is offline   samdark 

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

Posted 26 June 2017 - 01:00 PM

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

Enjoying Yii? Star us at github

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

#12 User is offline   aliceta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 13-November 17

Posted 14 November 2017 - 09:55 PM

In object-oriented computer programming, a Null Object is an object with no referenced value or with defined neutral ("null") behavior. The Null Object design pattern describes the uses of such objects and their behavior (or lack thereof). It was first published in the Pattern Languages of Program Design book series
0

#13 User is offline   samdark 

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

Posted 17 November 2017 - 05:13 AM

Thanks but we know what null object is.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: 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