fork download
  1. <?php
  2.  
  3. class Singleton
  4. {
  5. private static $instance;
  6. private function __construct() {} // Blokujemy domyślny konstruktor publiczny
  7. private function __clone(){} //Uniemożliwia utworzenie kopii obiektu
  8.  
  9. public static function getInstance ()
  10. {
  11. //Wersja czytelniejsza
  12. if (self::$instance === null) {
  13. self::$instance = new Singleton();
  14. }
  15. return self::$instance;
  16.  
  17. //Wersja z operatorem ternarnym
  18. return (self::$instance === null) ? self::$instance = new Singleton() : self::$instance;
  19. }
  20. }
  21.  
  22. // Pobieramy instancję
  23. $singletonNew = new Singleton();
  24. $singleton = Singleton::getInstance();
Runtime error #stdin #stdout #stderr 0.02s 52472KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Call to private Singleton::__construct() from invalid context in /home/kX3oLX/prog.php on line 23