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; } }