After upgrading PHP to version 7, why can't I use the mongodb driver?

Well, here is the strange situation I found. I use PHP and MongoDB together on a project and update the corresponding software. After the update, I definitely updated the new conf files, and everything is fine - except when I try to use the MongoClient class, I get the following fatal error:

Fatal error: impure error: class "MongoClient" not found in /srv/http/test.php

So my first instinct was to check my /etc/php/php.ini to make sure mongodb.so was turned on correctly. I found it after other extension lines:

 extension=mcrypt.so extension=zip.so extension=mongodb.so ; <--- here it is 

With this noted, I downloaded a quick phpinfo() script to verify that I used the correct php.ini and confirmed that I am. And not only that, the mongodb driver also looks properly loaded!

 Loaded Configuration File /etc/php/php.ini . . . mongodb support enabled mongodb version 1.1.1 mongodb stability stable libmongoc version 1.3.1-dev libbson version 1.3.0 

So, I restarted httpd and tried again. No dice, still unable to find the MongoClient class. So I tried installing it from pecl instead of my distribution package manager, and the situation is the same as described above.


So here is a summary of the situation:

  • Linux Kernel 4.3.3
  • PHP Version 7.0.1
  • php-mongodb version 1.1.1
  • It seems to be configured correctly and mongodb is enabled and shown in phpinfo() .
  • I tried installing the mongodb driver through pecl and my distribution package manager.

Additional information that may be helpful:

  • MongoDB Version 3.2.0
  • mongod works
  • Also tried to reboot, no change.
+5
source share
1 answer

Well, I found the answer.

The class has been changed. This is no longer \MongoClient and now \MongoDB\Driver\Manager

All I need is refactoring. The new driver is designed as a lower-level driver for higher-level functionality that must be populated with user-space libraries, so anyone else upgrading to php7, make sure that you are comfortable with the new driver or that the library user-space exists when you do it. Hope this question / answer will help others in the future.

The ianaz comment below posted this nice user space library for MongoDB: http://mongodb.imtqy.com/mongo-php-library/

+5
source

Source: https://habr.com/ru/post/1239853/


All Articles