<?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();