Created
March 18, 2014 03:46
-
-
Save kitelife/9613215 to your computer and use it in GitHub Desktop.
单例模式
This file contains hidden or 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 | |
| /* | |
| 当你使用单例模式第一次调用对象时,它就会被实例化(称为延迟加载),之后每一次调用都将返回同一个对象。 | |
| */ | |
| 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