Skip to content

Instantly share code, notes, and snippets.

@shawn-crigger
Created December 5, 2014 16:01
Show Gist options
  • Save shawn-crigger/e88bf5b46bd23388958f to your computer and use it in GitHub Desktop.
Save shawn-crigger/e88bf5b46bd23388958f to your computer and use it in GitHub Desktop.
Singleton Class wrapper for $wpdb
<?php
class DB
{
/**
* Returns the *Singleton* instance of this class.
*
* @staticvar Singleton $instance The *Singleton* instances of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function get_instance()
{
static $instance = null;
if ( NULL === $instance)
{
global $wpdb;
$instance = &$wpdb;
}
return $instance;
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct() { }
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone() { }
/**
* Private unserialize method to prevent unserializing of the *Singleton*
* instance.
*
* @return void
*/
private function __wakeup() { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment