Skip to content

Instantly share code, notes, and snippets.

@kitelife
Created March 18, 2014 03:46
Show Gist options
  • Select an option

  • Save kitelife/9613215 to your computer and use it in GitHub Desktop.

Select an option

Save kitelife/9613215 to your computer and use it in GitHub Desktop.
单例模式
<?php
/*
当你使用单例模式第一次调用对象时,它就会被实例化(称为延迟加载),之后每一次调用都将返回同一个对象。
*/
class Database extends PDO {
private static $_instance = null;
private function __construct()
{
parent::__construct(APP_DB_DSN, APP_DB_USER, APP_DB_PASSWORD);
}
public static function getInstance()
{
if (!(self::$_instance instanceof Database)) {
self::$_instance = new Database();
}
return self::$_instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment