


Sharing of instance operations of mongoDB singleton mode implemented in PHP
This article mainly introduces the mongoDB singleton mode operation class implemented by PHP. It analyzes the related implementation skills of the database encapsulation class of PHP based on the singleton mode to operate the MongoDB database in the form of examples. Friends who need it can refer to it. I hope it can help everyone. .
The following is the encapsulated code
class Mongo_db { private static $cli; /** * 不允许初始化 */ private function __construct() { $config = Config::get('config.mongo_config'); if(empty($config)){ $this->throwError('无法连接数据库!'); } if (!empty($config["user_name"])) { $this->mongo = new MongoClient("mongodb://{$config['user_name']}:{$config['password']}@{$config['host']}:{$config['port']}"); }else { $this->mongo = new MongoClient($config['host'] . ':' . $config['port']); } } /** * 单例模式 * @return Mongo|null */ public static function cli(){ if(!(self::$cli instanceof self)){ self::$cli = new self(); } return self::$cli->mongo; } } $mongo = Mongo_db::cli()->test->mycollection; // test 是选择的数据库 , mycollection 是选择的表。 因为使用单例模式,所以,只会实例一个资源具体操作再参考下面的文章吧
Related recommendations:
Detailed explanation of how php7 implements MongoDB fuzzy query
php method to implement singleton mode
The difference between factory mode and singleton mode in php design mode
The above is the detailed content of Sharing of instance operations of mongoDB singleton mode implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
