Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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