当前位置:网站首页>Single instance mode of encapsulating PDO with PHP in spare time
Single instance mode of encapsulating PDO with PHP in spare time
2022-07-06 02:43:00 【SinMu-L】
The following class is to record , Maybe you will use it when writing your own framework later . perform sql The logic of needs itself diy Oh .
The place where the database is connected can also be optimized
The main function :
- The singleton pattern ( The sluggard model )
- Prevent cloning
The call method is as follows
$res = Db::getInstance();
DB The categories are as follows
Class DB
{
// Singleton database class
private static $_instance;
private $pdo;
private $db_host = "127.0.0.1";
private $db_user = "root";
private $db_pwd = "123456";
private $db_name = "test";
private $charset = "utf8";
private $db_port = "3306";
// Prevent cloning objects
private function __clone()
{
}
// prevent new object private modification
private function __construct()
{
$this->connect();
$this->setCharSet();
}
/** * Connect to database */
private function connect()
{
try {
$this->pdo = new PDO("mysql:host=$this->db_host;port=$this->db_port;dbname=$this->db_name", $this->db_user, $this->db_pwd);
// echo " Successful connection ";
} catch (Exception $e) {
throw new Exception('MySql Connection error !');
// Write log logic ...
file_put_contents(__DIR__ . '/log/'.strftime('%Y-%m-%d').'.log',$e->getMessage().PHP_EOL,FILE_APPEND);
return false;
}
}
// initialization Db Class only needs to execute the static method
public static function getInstance(): Db
{
if (!(self::$_instance instanceof self)){
self::$_instance = new self();
}
return self::$_instance;
}
/** * Set character set */
private function setCharSet(){
$this->pdo->query($this->charset);
}
// perform sql sentence , No results returned
public function exec($sql){
// diy......
}
// perform select sentence , There are returned results
public function select(){
// diy....
}
}
added php Nostalgic knowledge , Can pay attention to 【php Developer community 】
边栏推荐
- Introduction to robotframework (III) Baidu search of webui automation
- Microservice registration and discovery
- 全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)
- 在GBase 8c数据库中使用自带工具检查健康状态时,需要注意什么?
- 不赚钱的科大讯飞,投资价值该怎么看?
- 如何精准识别主数据?
- MySQL (IV) - transactions
- 技术分享 | undo 太大了怎么办
- Data preparation
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
猜你喜欢

2345文件粉碎,文件强力删除工具无捆绑纯净提取版

一个复制也能玩出花来
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17](/img/85/2635afeb2edeb0f308045edd1f3431.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23](/img/72/a80ee7ee7b967b0afa6018070d03c9.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23

Ue4- how to make a simple TPS role (II) - realize the basic movement of the role

Pat grade a 1033 to fill or not to fill

Sword finger offer 29 Print matrix clockwise

Referenceerror: primordials is not defined error resolution

米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析

Introduction to robotframework (III) Baidu search of webui automation
随机推荐
Template_ Find the reverse pair of permutations_ Sort based on merge
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
Redis installation
A copy can also produce flowers
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.5 automatic differentiation_ Learning thinking and exercise answers
2345 file shredding, powerful file deletion tool, unbound pure extract version
MySQL winter vacation self-study 2022 11 (9)
继承的构造函数
I changed the driver to 5.1.35, but it is still the same error. I can succeed even now, but I will report this every time I do an SQL operation
微服务间通信
MySQL (IV) - transactions
Building the prototype of library functions -- refer to the manual of wildfire
Thinking on Architecture Design (under continuous updating)
How does yyds dry inventory deal with repeated messages in the consumption process?
有没有sqlcdc监控多张表 再关联后 sink到另外一张表的案例啊?全部在 mysql中操作
高数_向量代数_单位向量_向量与坐标轴的夹角
After changing the GCC version, make[1] appears in the compilation: cc: command not found
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
Large scale DDoS attacks take Myanmar offline