Skip to main content
Tweeted twitter.com/#!/StackProgrammer/status/500229386965377024
deleted 43 characters in body
Source Link
acid
  • 159
  • 2

I am wondering how are you guys handling an optional class properties. Let's say I have a product that can bud doesn't have to have a relatedProductscolor arrayproperty. Is that really the best way of doing that? Should I use null object pattern? Should I use strategy pattern maybe? Should I maybe use a decorator pattern here so I'll have Product and ProductWithRelatedProductsProductWithColor? What if another property will be added and become optional?

<?php class Product { private $id; private $title; /** * @var array|null */ private $relatedProducts; function __construct($id, $title, array $relatedProducts = []) { $this->id = $id; $this->relatedProducts = $relatedProducts; $this->title = $title; } } 
<?php class Product { private $id; private $title; /** * @var string|null */ private $color; function __construct($id, $title, array $color = null) { $this->id = $id; $this->title = $title; $this->color = $color; } } 

I am wondering how are you guys handling an optional class properties. Let's say I have a product that can bud doesn't have to have a relatedProducts array. Is that really the best way of doing that? Should I use null object pattern? Should I use strategy pattern maybe? Should I maybe use a decorator pattern here so I'll have Product and ProductWithRelatedProducts? What if another property will be added and become optional?

<?php class Product { private $id; private $title; /** * @var array|null */ private $relatedProducts; function __construct($id, $title, array $relatedProducts = []) { $this->id = $id; $this->relatedProducts = $relatedProducts; $this->title = $title; } } 

I am wondering how are you guys handling an optional class properties. Let's say I have a product that can bud doesn't have to have a color property. Is that really the best way of doing that? Should I use null object pattern? Should I use strategy pattern maybe? Should I maybe use a decorator pattern here so I'll have Product and ProductWithColor? What if another property will be added and become optional?

<?php class Product { private $id; private $title; /** * @var string|null */ private $color; function __construct($id, $title, array $color = null) { $this->id = $id; $this->title = $title; $this->color = $color; } } 
Source Link
acid
  • 159
  • 2

Dealing with class optional dependencies

I am wondering how are you guys handling an optional class properties. Let's say I have a product that can bud doesn't have to have a relatedProducts array. Is that really the best way of doing that? Should I use null object pattern? Should I use strategy pattern maybe? Should I maybe use a decorator pattern here so I'll have Product and ProductWithRelatedProducts? What if another property will be added and become optional?

<?php class Product { private $id; private $title; /** * @var array|null */ private $relatedProducts; function __construct($id, $title, array $relatedProducts = []) { $this->id = $id; $this->relatedProducts = $relatedProducts; $this->title = $title; } }