Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\Client::__get()

On this page

  • Definition
  • Parameters
  • Return Values
  • Behavior
  • Examples
  • See Also
MongoDB\Client::__get()

Selects a database on the server. This magic method is an alias for the selectDatabase() method.

function __get(string $databaseName): MongoDB\Database
$databaseName : string
The name of the database to select.

A MongoDB\Database object.

The selected database inherits options such as read preference and type mapping from the Client object. If you wish to override any options, use the MongoDB\Client::selectDatabase() method.

Note

To select databases whose names contain special characters, such as -, use complex syntax, as in $client->{'that-database'}.

Alternatively, MongoDB\Client::selectDatabase() supports selecting databases whose names contain special characters.

The following example selects the test and another-app databases:

<?php
$client = new MongoDB\Client;
$test = $client->test;
$anotherApp = $client->{'another-app'};
← MongoDB\Client::__construct()