PHP Singleton PDO

后端 未结 5 1622
臣服心动
臣服心动 2021-02-09 08:45

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         


        
5条回答
  •  不要未来只要你来
    2021-02-09 09:17

    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.

提交回复
热议问题