SHARE
TWEET

Untitled

a guest Jan 12th, 2017 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
Top