Last active
August 29, 2015 14:01
-
-
Save ihsanberahim/1c19dd54b004bd39be31 to your computer and use it in GitHub Desktop.
Simple Basic Singleton
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
class App | |
{ | |
public static $instance = null; | |
public $instance_id = null; | |
public $db; | |
function __construct() | |
{ | |
} | |
public static function getInstance() | |
{ | |
if(self::$instance==null) | |
{ | |
self::$instance = new App; | |
self::$instance->instance_id = time(); | |
self::$instance->db = DB::getInstance(); | |
} | |
return self::$instance; | |
} | |
} |
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
class DB | |
{ | |
public static $instance = null; | |
public $instance_id = null; | |
function __construct() | |
{ | |
} | |
public static function getInstance() | |
{ | |
if(self::$instance==null) | |
{ | |
self::$instance = new DB; | |
self::$instance->instance_id = time(); | |
} | |
return self::$instance; | |
} | |
} |
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 | |
require_once 'App.class.inc'; | |
require_once 'DB.class.inc'; | |
$app = App::getInstance(); | |
$app2 = new App(); | |
$db = DB::getInstance(); | |
?> | |
<pre><?php var_dump($app) ?></pre> | |
<pre><?php var_dump($db) ?></pre> | |
<pre><?php var_dump($app2) ?></pre> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment