Skip to content

Instantly share code, notes, and snippets.

@ihsanberahim
Last active August 29, 2015 14:01
Show Gist options
  • Save ihsanberahim/1c19dd54b004bd39be31 to your computer and use it in GitHub Desktop.
Save ihsanberahim/1c19dd54b004bd39be31 to your computer and use it in GitHub Desktop.
Simple Basic Singleton
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;
}
}
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;
}
}
<?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