3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class App { public function run() { echo 'Hello' . PHP_EOL; } } class AppContainer { private static $app = null; public static function getInstance() { if (null === self::$app) { self::$app = self::makeInstance(); } return self::$app; } private static function makeInstance() { $app = new App(); // do all logic for adding routes etc return $app; } } // Whenever you need it $app1 = AppContainer::getInstance(); $app2 = AppContainer::getInstance(); if ($app1 === $app2) { echo 'yay, same object' . PHP_EOL; } $app1->run(); $app2->run();
based on Bddr8
Output for 5.3.21 - 7.3.1
yay, same object Hello Hello