PHP Singleton PDO

后端 未结 3 763
滥情空心
滥情空心 2021-02-09 08:50

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         


        
3条回答
  •  攒了一身酷
    2021-02-09 09:21

    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.

提交回复
热议问题