PHP Singleton PDO [英] PHP Singleton PDO

查看:106
本文介绍了PHP Singleton PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.php.net/manual/en/class .pdo.php

  ###### config.ini ###### 
db_driver = mysql
db_user = root
db_password = 924892xp

[dsn]
host = localhost
port = 3306
dbname = localhost

[db_options]
PDO :: MYSQL_ATTR_INIT_COMMAND =设置名称utf8

[db_attributes]
ATTR_ERRMODE = ERRMODE_EXCEPTION
##### #######

<?php class Database {
private static $ link = null;

私有静态函数getLink(){
if(self :: $ link){
return self :: $ link;
}

$ ini = _BASE_DIR。 config.ini;
$ parse = parse_ini_file($ ini,true);

$ driver = $ parse [db_driver];
$ dsn =$ {driver}:;
$ user = $ parse [db_user];
$ password = $ parse [db_password];
$ options = $ parse [db_options];
$ attributes = $ parse [db_attributes];

foreach($ parse [dsn] as $ k => $ v){
$ dsn。=$ {k} = $ {v}; ;
}

self :: $ link = new PDO($ dsn,$ user,$ password,$ options);

foreach($ attributes as $ k => $ v){
self :: $ link - > setAttribute(constant(PDO :: {$ k})
,常量(PDO :: {$ v}));
}

return self :: $ link;
}

public static function __callStatic($ name,$ args){
$ callback = array(self :: getLink(),$ name);
return call_user_func_array($ callback,$ args);
}
}?>

<?php // examples
$ stmt = Database :: prepare(SELECTsomething';);
$ stmt - >执行 ( ) ;
var_dump($ stmt - > fetchAll());
$ stmt - > closeCursor();
?>

我的问题是:



单身吗?



什么是静态意思/做?



什么是公共静态函数__callStatic(用于? / p>

我该怎么做,PDO只在需要时才连接到数据库?如查询或转义?所以如果类/对象没有使用,

解决方案

单例是一种软件设计模式,将类的启动限制为一个实例。 http://en.wikipedia.org/wiki/Singleton_pattern



静态意味着某些属于类而不是特定的实例,在PHP中,这也意味着需要使用 :: 而不是 - >



_callStatic如果已经建立,返回PDO链接,否则首先创建链接,然后返回。



您的第四个问题的答案正是单身形式。它确保连接仅在需要时设置一次。


from http://www.php.net/manual/en/class.pdo.php

###### config.ini ######
db_driver=mysql
db_user=root
db_password=924892xp

[dsn]
host=localhost
port=3306
dbname=localhost

[db_options]
PDO::MYSQL_ATTR_INIT_COMMAND=set names utf8

[db_attributes]
ATTR_ERRMODE=ERRMODE_EXCEPTION
############

<?php class Database {
    private static $link = null ;

    private static function getLink ( ) {
        if ( self :: $link ) {
            return self :: $link ;
        }

        $ini = _BASE_DIR . "config.ini" ;
        $parse = parse_ini_file ( $ini , true ) ;

        $driver = $parse [ "db_driver" ] ;
        $dsn = "${driver}:" ;
        $user = $parse [ "db_user" ] ;
        $password = $parse [ "db_password" ] ;
        $options = $parse [ "db_options" ] ;
        $attributes = $parse [ "db_attributes" ] ;

        foreach ( $parse [ "dsn" ] as $k => $v ) {
            $dsn .= "${k}=${v};" ;
        }

        self :: $link = new PDO ( $dsn, $user, $password, $options ) ;

        foreach ( $attributes as $k => $v ) {
            self :: $link -> setAttribute ( constant ( "PDO::{$k}" )
                , constant ( "PDO::{$v}" ) ) ;
        }

        return self :: $link ;
    }

    public static function __callStatic ( $name, $args ) {
        $callback = array ( self :: getLink ( ), $name ) ;
        return call_user_func_array ( $callback , $args ) ;
    }
} ?>

<?php // examples
$stmt = Database :: prepare ( "SELECT 'something' ;" ) ;
$stmt -> execute ( ) ;
var_dump ( $stmt -> fetchAll ( ) ) ;
$stmt -> closeCursor ( ) ;
?>

My questions are:

What is singleton?

What does static mean/do?

What is public static function __callStatic ( used for?

And how can I make it, that PDO only connects to the database when needed? Such as a query or escaping? So if the class/object is unused then it doesn't connect.

解决方案

A singleton is a software design pattern that restricts the initiation of a class to one instance. http://en.wikipedia.org/wiki/Singleton_pattern

Static means that something belongs to the class and not a particular instance. In PHP, this also means that a static method needs to be called with :: not ->

_callStatic returns the PDO link if it has already been established. Otherwise, it first creates the link and then returns it.

The answer to your fourth question is precisely the singleton pattern. It ensures that the connection is only set up once, when needed.

这篇关于PHP Singleton PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆