PHP Singleton 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:

What is singleton?

What does the static value of / do mean?

What is the __callStatic public static function (used for?

And how can I do this, that PDO only connects to the database when necessary? For example, a request or an escape? Therefore, if the class / object is not used, it is not connected.

+7
php design-patterns pdo singleton
source share
4 answers

Syntax is a software development pattern that restricts class initialization to a single instance. http://en.wikipedia.org/wiki/Singleton_pattern

Staticity means that something belongs to a class, not a specific instance. In PHP, this also means that the static method must be called with :: not ->

_callStatic returns a PDO link if it is already installed. Otherwise, it first creates the link and then returns it.

The answer to your fourth question is just a singleton pattern. This ensures that the connection is established only once, when necessary.

+4
source share

One ton is a static function that allows you to track instances of an object when you use the singleton that you instantiate the object, but instances always remain with the associated object.

Take this example:

 $db1 = new Database(); $db2 = new Database(); 

since you can see that db1 and db2 are 2 new database instances, so itโ€™s not the same there, now take this example.

 $db1 = Database::Instance(); $db2 = Database::Instance(); 

And the code for the instance

 class Database { private static $_instance; public static Instance() { if(self::$_instance !== null) { //We have already stored the object locally so just return it. //This is how the object always stays the same return self::$_instance; } return self::$_instance = new Database(); //Set the instance. } } 

If you analyze the code, you will be so that no matter where you use the instance throughout the application, your object will always be the same.

a static function is a method inside a class / object is a type of method that can be used without initializing the object.

As for the __callStatic method, this is the Magic Method, which runs where the static method is not available.

For example:

 class Database { public static function first() { echo 'I actually exists and I am first'; } public function __callStatic($name,$args) { echo 'I am '. $name .' and I was called with ' . count($args) . ' args'; } } 

lets check them out.

 Database::first(); //Output: I actually exists and I am first Database::second(); //Output: I am second and I was called with 0 args Database::StackOverflow(true,false); //Output: I am StackOverflow and I was called with 2 args 

Hope this helps you

+3
source share

Singleton means that you have this class that will have only one object associated with it. Thus, this class will be created only once, after which you will call a method with a type name getInstance() , which will return your instance.

Static means that you do not need an instance of this object to access this method or property , you can call this method directly with your class class::myMethod()

__callStatic is a way to access a static method. It is one of the PHP magic-methods and is similar to __call with the difference I just mentioned.

For the last question, this class receives only one database connection. If you add another method, for example, executeSelect($sql) , you will have something like:

 public static function executeSelect($sql) { if (self::$link) { self::getLink() } // execute your query here... return null; } 

You will need to call it as: Database::executeSelect($sql); , and he will never get two DB connections at the same time.

0
source share

Syntax is an object that ensures that only one instance in itself is active at a time. That is, you can only do one program if you know what I mean.

A static method is a method that can be called as a normal function, outside the context of an object. Like SomeClass:afunc() and not only $this->afunc() .

0
source share

All Articles