28

When executing the following PHP code:

$m = new MongoClient("mongodb://localhost:27017");

I get the following error:

Fatal error: Class 'MongoClient' not found in (...)

MongoDB extension seems properly installed (I copied php_mongodb.dll to ext folder and updated php.ini).

PHP seems to confirm that the extension is running properly as the following code confirms it is loaded:

echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n";

Also, phpinfo() shows that mongodb extension has been loaded.


UPDATE: my problem is still not solved.

phpinfo() clearly shows that the driver is loaded:

enter image description here

But I am still receiving the same fatal error.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Tom
  • 1,375
  • 3
  • 24
  • 45
  • 3
    MongoDB != MongoClient – Charlotte Dunois May 16 '16 at 19:42
  • http://php.net/MongoDB http://php.net/MongoClient – Charlotte Dunois May 16 '16 at 19:42
  • Thanks a lot for clarifying this! How can I install the MongoDB library for PHP on WIndows (official instructions are for Linux only)? – Tom May 16 '16 at 20:20
  • In addition, official instructions mae use of "pecl" command which I don't have... – Tom May 16 '16 at 20:21
  • You installed MongoDB, according to your question. You only need to use the correct class now. ;) – Charlotte Dunois May 16 '16 at 20:21
  • `Application developers should consider using this extension in conjunction with the » MongoDB PHP library, which implements the same higher level APIs found in MongoDB drivers for other languages.` http://github.com/mongodb/mongo-php-library – Charlotte Dunois May 16 '16 at 20:22
  • Thanks! and that's exactly what I'm trying to do but can't figure out how to install the MongoDB PHP lib (since I seem to have installed the basic driver only) – Tom May 16 '16 at 20:30
  • Warning: This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include: MongoDB\Driver\Manager - http://php.net/MongoClient. Why don't you just use `$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");`, as the documentation is highly suggesting you to do ? – β.εηοιτ.βε May 19 '16 at 11:34

2 Answers2

93

TL;DR

The class MongoClient is part of the legacy PECL package mongo but not anymore of the up-to-date mongodb package.

And since you have the mongodb extension installed, and not the mongo one, this is why you are getting the error

Fatal error: Class 'MongoClient' not found

On MongoDB PHP driver github repo, the release note about the version 1.0.0, is suggesting developers to use MongoDB\Driver\Manager instead of MongoClient

Changes from our legacy mongo extension

Most significantly, the legacy driver's MongoClient, MongoDB, and MongoCollection classes have been obsoleted by the MongoDB\Driver\Manager class, which is the new gateway for connecting and executing queries, commands, and write operations.

Source:: https://github.com/mongodb/mongo-php-driver/releases/tag/1.0.0

So, here is the replacement class documentation and the snippet of code that should replace yours :

$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");

As the documentation is prompting it, the class is deprecated.

Warning This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:

Source: http://php.net/MongoClient


From what I read on their github repository release history, the class you are trying to use have been obsoleted since the version of mongodb 1.0.0, so, on the version 1.6.0 you are, this class is not even part of the dll anymore.

That is confirmed by this issue on their github

derickr commented on Apr 16

MongoClient is a class from the old legacy driver and is not supposed to be available in this one. The new driver has \MongoDB\Driver\Manager, and, the accompanying library has \MongoDB\Client.

You either need to install the old legacy extension (pecl install mongo) and use PHP 5.x, or update your code to use this new driver's classes as the old driver is not available for PHP 7. There is an upgrade guide at http://mongodb.github.io/mongo-php-library/upgrade-guide/

Source: https://github.com/mongodb/mongo-php-driver/issues/300#issuecomment-210820288


Another way, as suggested by the MongoDB member quoted here above is to use this pecl extension: https://pecl.php.net/package/mongo instead of https://pecl.php.net/package/mongodb but please also notice the warning there stating:

This package has been superseded, but is still maintained for bugs and security fixes.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 1
    Thanks a lot, there are lots of pages said to be "deprecated" and refers to another page which also deprecated.. Examples in the PHP manual shows "$m = new MongoClient()" which doesn't work anymore, I also tried my chance with "$m = new MongoDB()" which also doesn't work too. So this solved my problem. Thanks. – endo64 Jan 19 '17 at 13:32
0

MongoDB(mongo-php-library) and MongoClient(ext-mongo) are different extensions. MongoClient extension is deprecated. If you want to use MongoClient(ext-mongo) related classes, use a wrapper like this one;

https://github.com/mitsh/mongo-php-adapter

Adapter to provide ext-mongo interface on top of mongo-php-library

You don't need to change anything on your project. Just install and include it with composer.

MIT
  • 140
  • 1
  • 5
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30275564) – Adrian Mole Nov 06 '21 at 22:25