MongoDBClient__get.txt 1.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
========================
MongoDB\\Client::__get()
========================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Client::__get()
17

18 19 20
   Selects a database on the server. This :php:`magic method <oop5.magic>` is
   an alias for the :phpmethod:`selectDatabase()
   <MongoDB\\Client::selectDatabase>` method.
21 22 23 24 25

   .. code-block:: php

      function __get($databaseName): MongoDB\Database

26
   This method has the following parameters:
27 28 29

   .. include:: /includes/apiargs/MongoDBClient-method-get-param.rst

30 31
Return Values
-------------
32

33
A :phpclass:`MongoDB\\Database` object.
34

35 36 37
Behavior
--------

38 39 40
The selected database inherits options such as read preference and type mapping
from the :phpclass:`Client <MongoDB\\Client>` object. If you wish to override
any options, use the :phpmethod:`MongoDB\\Client::selectDatabase` method.
41 42

.. note::
43

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

   Alternatively, :phpmethod:`MongoDB\\Client::selectDatabase()` supports
48 49 50 51 52
   selecting databases whose names contain special characters.

Examples
--------

53
The following example selects the ``test`` and ``another-app`` databases:
54 55 56 57 58 59 60

.. code-block:: php

   <?php

   $client = new MongoDB\Client;

61
   $test = $client->test;
62 63
   $anotherApp = $client->{'another-app'};

64 65
See Also
--------
66

67 68 69
- :phpmethod:`MongoDB\\Client::selectDatabase()`
- :phpmethod:`MongoDB\\Database::__construct()`
- :php:`Property Overloading <oop5.overloading>` in the PHP Manual