PHP 8.3.4 Released!

Класс MongoDB\Driver\Manager

(mongodb >=1.0.0)

Введение

Класс MongoDB\Driver\Manager является основной точкой входа в модуль. Он отвечает за поддержание соединений с MongoDB (будь то автономный сервер, набор реплик или распределённый кластер).

При инициализации класса Manager не происходит подключение к MongoDB. Это означает, что MongoDB\Driver\Manager всегда может быть создан, даже если сервера MongoDB не работают.

Любая запись или запрос будут выбрасывать исключение соединения, поскольку соединения создаются "лениво" (по требованию). Сервер MongoDB также может быть недоступен в течение всего времени выполнения скрипта. Поэтому важно, чтобы все действия с Manager были обёрнуты в блок try/catch.

Обзор классов

final class MongoDB\Driver\Manager {
/* Методы */
final public __construct(?string $uri = null, ?array $uriOptions = null, ?array $driverOptions = null)
final public getServers(): array
}

Примеры

Пример #1 Простой пример использования MongoDB\Driver\Manager::__construct()

Выводит различную информацию о MongoDB\Driver\Manager с помощью функции var_dump(). Это может быть полезно для отладки, чтобы посмотреть как драйвер видит настройку MongoDB и какие опции используются.

<?php

$manager
= new MongoDB\Driver\Manager('mongodb://localhost:27017');
var_dump($manager);

?>

Вывод приведённого примера будет похож на:

object(MongoDB\Driver\Manager)#1 (2) {
  ["uri"]=>
  string(26) "mongodb://127.0.0.1:27017/"
  ["cluster"]=>
  array(0) {
  }
}

Содержание

add a note

User Contributed Notes 3 notes

up
8
mike at eastghost dot com
5 years ago
According to Mongo, this (i.e., MongoDB\Driver\Manager) is an "entry point" for the extension:

"This class serves as an entry point for the MongoDB PHP Library. It is the preferred class for connecting to a MongoDB server or cluster of servers and acts as a gateway for accessing individual databases and collections. MongoDB\Client is analogous to the driver’s MongoDB\Driver\Manager class, which it composes."

copied from here: https://docs.mongodb.com/php-library/master/reference/class/MongoDBClient/

However, any comparison of the "mongodb" docs here on php.net versus the "mongodb driver" docs on mongo's site shows dramatic and ever-changing differences.
up
-11
gbormann dot nospam at telenet dot be
4 years ago
Re mocking implications of final nature of MongoDB\Driver\Manager :
Final classes can be mocked through an indirection using a wrapper class.
up
-20
Zalmoksis
5 years ago
It's worth noting that Manager is final, so you cannot mock it easily with tools like PhpUnit.
To Top