×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Franz Vinzenz
Added: May 7, 2015 3:01 PM
Views: 11
Tags: no tags
http://stackoverflow.com/questions/4302710/static-instance-in-php
  1. class SingletonClass {
  2.     protected $instance = null;
  3.     protected $var = 3;
  4.     protected __construct () {}
  5.     protected __clone() {}
  6.     public static function getInstance () {
  7.         if (is_null(self::$instance)) { self::$instance = new self(); }
  8.         return self::$instance;
  9.     }
  10.     public function doSomething () {
  11.         $this->var++;
  12.         echo $this->var;
  13.     }
  14. }
  15. $a = SingletonClass::getInstance();
  16. $a->doSomething();