Mar*_*aio 24 php database oop singleton static
我并不想创建一个关于单比静态比全球要好,等我看了几十个关于SO类似问题的问题,但我不能想出一个回答这个具体问题更好的讨论,所以我希望有人可以用一个(或多个)真实的简单例子来回答这个问题,而不仅仅是理论上的讨论.
在我的应用程序中,我有一个典型的DB类来抽象数据库层并在数据库上执行任务而无需在代码中随处写入mysql_connect / mysql_select_db / mysql...
我可以把这个类写成静态类:
class DB
{
private static $connection = FALSE; //connection to be opened
//DB connection values
private static $server = NULL; private static $usr = NULL; private static $psw = NULL; private static $name = NULL;
public static function init($db_server, $db_usr, $db_psw, $db_name)
{
//simply stores connections values, without opening connection
}
public static function query($query_string)
{
//performs query over alerady opened connection, if not open, it opens connection 1st
}
...
}
Run Code Online (Sandbox Code Playgroud)
或作为SINGLETON:
class DBSingleton
{
private $inst = NULL;
private $connection = FALSE; //connection to be opened
//DB connection values
private $server = NULL; private $usr = NULL; private $psw = NULL; private $name = NULL;
public static function getInstance($db_server, $db_usr, $db_psw, $db_name)
{
//simply stores connections values, without opening connection
if($inst === NULL)
$this->inst = new DBSingleton();
return $this->inst;
}
private __construct()...
public function query($query_string)
{
//performs query over already opened connection, if connection is not open, it opens connection 1st
}
...
}
Run Code Online (Sandbox Code Playgroud)
然后在我的应用程序后,如果我想查询数据库,我可以做
//Performing query using static DB object
DB:init(HOST, USR, PSW, DB_NAME);
DB::query("SELECT...");
//Performing query using DB singleton
$temp = DBSingleton::getInstance(HOST, USR, PSW, DB_NAME);
$temp->query("SELECT...");
Run Code Online (Sandbox Code Playgroud)
对我来说,Singleton有唯一的优势,可以避免声明为static类的每个方法.我敢肯定,你们中的一些可以给我一个实例的单身的真正优势在这种特定的情况下.提前致谢.
以下(简化)示例有什么问题:
class Database
{
protected $_connection;
protected $_config;
public function __construct( array $config ) // or other means of passing config vars
{
$this->_config = $config;
}
public function query( $query )
{
// use lazy loading getter
return $this->_getConnection()->query( $query );
}
protected function _getConnection()
{
// lazy load connection
if( $this->_connection === null )
{
$dsn = /* create valid dsn string from $this->_config */;
try
{
$this->_connection = new PDO( $dsn, $this->_config[ 'username' ], $this->_config[ 'password' ] );
}
catch( PDOException $e )
{
/* handle failed connecting */
}
}
return $this->_connection;
}
}
$db1 = new Database( array(
'driver' => 'mysql',
'host' => 'localhost',
'dbname' => 'test',
'username' => 'test_root',
'password' => '**********'
) );
$db2 = new Database( array(
'driver' => 'pgsql',
'host' => '213.222.1.43',
'dbname' => 'otherdb',
'username' => 'otherdb_root',
'password' => '**********'
) );
$someModel = new SomeModel( $db1 );
$someOtherModel = new SomeOtherModel( $db2 );
$yetAnotherModel = new YetAnotherModel( $db2 );
Run Code Online (Sandbox Code Playgroud)
这演示了如何使用延迟加载连接,并且仍然可以灵活地使用不同的数据库连接.
当使用其中一个实例(在本例中为其中一个模型)的对象决定调用实例的方法时,数据库实例将仅连接到它们各自的连接.
| 归档时间: |
|
| 查看次数: |
11915 次 |
| 最近记录: |