<?php class SingleInstance { private static $instances = []; public static function load($class, $args = null) { if ($args) { $args = implode(', ', $args); } $c = new $class($args); if (in_array($c, self::$instances)) { echo "Hit!"; return self::$instances[$class]; } self::$instances[$class] = $c; return self::$instances[$class]; } } class Words { private $word; public function __construct($word) { $this->word = $word; } public function show() { return $this->word; } } $a = SingleInstance::load('Words', ['Dog']); echo $a->show().PHP_EOL; $b = SingleInstance::load('Words', ['Cat']); echo $b->show().PHP_EOL; $c = SingleInstance::load('Words', ['Cat']); echo $c->show().PHP_EOL;
You have javascript disabled. You will not be able to edit any code.