当前位置:网站首页>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 】
边栏推荐
- Universal crud interface
- Day 50 - install vsftpd on ceontos6.8
- Introduction to robotframework (III) Baidu search of webui automation
- Redis skip table
- Initial understanding of pointer variables
- 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
- Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
- Pat 1046 shortest distance (20 points) simulation
- Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
- 在GBase 8c数据库中使用自带工具检查健康状态时,需要注意什么?
猜你喜欢
MySQL winter vacation self-study 2022 11 (9)
PMP每日一练 | 考试不迷路-7.5
Maturity of master data management (MDM)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
【Unity3D】GUI控件
2022 China eye Expo, Shandong vision prevention and control exhibition, myopia, China myopia correction Exhibition
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
随机推荐
Apt installation ZABBIX
Number conclusion LC skimming review - 1
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
Initial understanding of pointer variables
Network Security Learning - Web vulnerabilities (Part 1)
Elimination games
故障分析 | MySQL 耗尽主机内存一例分析
球面透镜与柱面透镜
2.12 simulation
原型图设计
MySQL winter vacation self-study 2022 11 (8)
Sword finger offer 29 Print matrix clockwise
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
Dachang image library
高数_向量代数_单位向量_向量与坐标轴的夹角
力扣今日题-729. 我的日程安排表 I
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10