Defining the Connection class

The main task of the Connection class is to produce an instance of MongoDB\Client. We do not show the class constants which represent error messages in order to conserve space. We start by defining the namespace and external classes to use:

namespace Application; use Exception; use MongoDB\Client as MongoClient; class Connection {

The __construct() method accepts a configuration array as an argument, and produces the MongoDB\Client and MongoDB\Driver\Manager instances:

    protected $mongoClient;     protected $manager;     // MongoDB\Driver\Manager     public function __construct($config) {         if (!isset($config['uri'])) {             throw new Exception(self::ERROR_CONFIG_URI);         }         $uriOpts = $config['uriOpts'] ?? []; $driverOpts = $config['driverOpts'] ...

Get MongoDB 4 Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.