当前位置:网站首页>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 】
边栏推荐
- 力扣今日題-729. 我的日程安排錶 I
- Differences and usage scenarios between TCP and UDP
- 会员积分营销系统操作的时候怎样提升消费者的积极性?
- MySQL (IV) - transactions
- Elimination games
- 纯Qt版中国象棋:实现双人对战、人机对战及网络对战
- Patch NTP server at the beginning of DDoS counterattack
- 一个复制也能玩出花来
- UE4 - how to make a simple TPS role (I) - create a basic role
- 解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
C language - Blue Bridge Cup - promised score
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Advanced technology management - what is the physical, mental and mental strength of managers
力扣今日題-729. 我的日程安排錶 I
UE4 - how to make a simple TPS role (I) - create a basic role
有没有完全自主的国产化数据库技术
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
随机推荐
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
深度解析链动2+1模式,颠覆传统卖货思维?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
事故指标统计
Accident index statistics
力扣今日題-729. 我的日程安排錶 I
Microservice registration and discovery
Technology sharing | what if Undo is too big
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.3 linear algebra_ Learning thinking and exercise answers
有没有sqlcdc监控多张表 再关联后 sink到另外一张表的案例啊?全部在 mysql中操作
主数据管理理论与实践
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
UE4 - how to make a simple TPS role (I) - create a basic role
Six stone management: why should leaders ignore product quality
RobotFramework入门(二)appUI自动化之app启动
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
Dachang image library
DDoS "fire drill" service urges companies to be prepared
SQL table name is passed as a parameter