Created
November 21, 2013 19:50
-
-
Save egulhan/7588358 to your computer and use it in GitHub Desktop.
Getting instance of an class via static function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SampleClass | |
{ | |
static private $instance; | |
/** | |
* This implements the 'singleton' design pattern | |
*/ | |
static function getInstance() | |
{ | |
if (!self::$instance) { | |
self::$instance = new SampleClass(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Private constructor | |
*/ | |
private function __construct() | |
{ | |
/* ... */ | |
} | |
/* ... */ | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment