当前位置:网站首页>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 】
边栏推荐
- 我把驱动换成了5.1.35,但是还是一样的错误,我现在是能连成功,但是我每做一次sql操作都会报这个
- 不赚钱的科大讯飞,投资价值该怎么看?
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
- High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
- Qt发布exe软件及修改exe应用程序图标
- [postgraduate entrance examination English] prepare for 2023, learn list5 words
- 在GBase 8c数据库中使用自带工具检查健康状态时,需要注意什么?
- [Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
- Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
猜你喜欢
【若依(ruoyi)】设置主题样式
Network Security Learning - Web vulnerabilities (Part 1)
2022.02.13
Solution: attributeerror: 'STR' object has no attribute 'decode‘
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
主数据管理(MDM)的成熟度
CobaltStrike-4.4-K8修改版安装使用教程
2345 file shredding, powerful file deletion tool, unbound pure extract version
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
随机推荐
事故指标统计
[untitled] a query SQL execution process in the database
Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
大厂镜像库
Universal crud interface
2022.02.13
Master data management theory and Practice
Briefly describe the implementation principle of redis cluster
Bigder: I felt good about the 34/100 interview, but I didn't receive the admission
CSP date calculation
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Trends in DDoS Attacks
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
原型图设计
How to check the lock information in gbase 8C database?
不赚钱的科大讯飞,投资价值该怎么看?
There are so many giants, why should we independently develop POS store cashier system?
Redis installation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13