Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

What you have described is essentially the singleton pattern. Please see this questionsee this question for good reasons why you might not want to do this.

If you really want to do it, you could implement something like this:

class a { public static $instance; public function __construct() { self::$instance = $this; } public static function get() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } } $a = a::get(); 

What you have described is essentially the singleton pattern. Please see this question for good reasons why you might not want to do this.

If you really want to do it, you could implement something like this:

class a { public static $instance; public function __construct() { self::$instance = $this; } public static function get() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } } $a = a::get(); 

What you have described is essentially the singleton pattern. Please see this question for good reasons why you might not want to do this.

If you really want to do it, you could implement something like this:

class a { public static $instance; public function __construct() { self::$instance = $this; } public static function get() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } } $a = a::get(); 
added 229 characters in body
Source Link
Tom Haigh
  • 57.8k
  • 21
  • 119
  • 143

CanWhat you modifyhave described is essentially the constructor? Sosingleton pattern. Please see this question for good reasons why you might not want to do this.

If you really want to do it, you could haveimplement something like this:

class a { public static $instance; public function __construct() { self::$instance = $this; } }  public static function get() {  if ($aself::$instance ==== anull) { self::$instance = new self();  { } //do something with existing instancereturn ofself::$instance;  a } } $a = a::get(); 

Can you modify the constructor? So you could have:

class a { public static $instance; public function __construct() { self::$instance = $this; } } if ($a = a::$instance) { //do something with existing instance of a } 

What you have described is essentially the singleton pattern. Please see this question for good reasons why you might not want to do this.

If you really want to do it, you could implement something like this:

class a { public static $instance; public function __construct() { self::$instance = $this; }  public static function get() {  if (self::$instance === null) { self::$instance = new self();   } return self::$instance;   } } $a = a::get(); 
Source Link
Tom Haigh
  • 57.8k
  • 21
  • 119
  • 143

Can you modify the constructor? So you could have:

class a { public static $instance; public function __construct() { self::$instance = $this; } } if ($a = a::$instance) { //do something with existing instance of a }