Как реализовать вывод одного и того же рандомного числа при помощи SingleTon?

Ожидается вывод одного и того же рандомного числа 4 раза. А в результате только один раз.Вот код:

class Single{
    private static $instance = null;
    public $result;
    private function __construct(){
        $this->result  = random_int(100, 999);
        $this->getresult();
        }
    protected function getresult(){
        echo "Result:" . $this->result;
    }
    public static function getInstance(){
        return static::$instance ?: static::$instance = new static();
    }
}
Single::getInstance();
Single::getInstance();
Single::getInstance();
Single::getInstance();

Ответы (1 шт):

Автор решения: Тимофей Рябогин
class Single{
    private static $instance = null;
    public $result;
    public function __construct(){
        $this->result  = random_int(100, 999);
        $this->getresult();
        }
    protected function getresult(){
        echo "Result:" . $this->result;
    }
    public static function getInstance(){
        return static::$instance ?? static::$instance = new static();
        // return static::$instance ?: static::$instance = new static();
    }
}
new Single();

new Single();

new Single();
new Single();
→ Ссылка