Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. <?php
  2. trait Singleton
  3. {
  4. private static $instance;
  5.  
  6. private function __construct() {}
  7.  
  8. public static function getInstance()
  9. {
  10. if (!(self::$instance instanceof self)) {
  11. self::$instance = new self;
  12. }
  13. return self::$instance;
  14. }
  15.  
  16. }
  17.  
  18. class MyClass
  19. {
  20. use Singleton;
  21. }
  22.  
  23. $object = MyClass::getInstance(); // object of type MyClass
  24.  
  25. $object = new MyClass; // PHP fatal error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement