MongoDB:致命错误:找不到“MongoClient"类 [英] MongoDB: Fatal error: Class 'MongoClient' not found

查看:189
本文介绍了MongoDB:致命错误:找不到“MongoClient"类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下 PHP 代码时:

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

我收到以下错误:

<块引用>

致命错误:在 (...) 中找不到类MongoClient"

MongoDB 扩展似乎已正确安装(我将 php_mongodb.dll 复制到 ext 文件夹并更新了 php.ini).

PHP 似乎确认扩展程序运行正常,因为以下代码确认它已加载:

echo extension_loaded("mongodb") ?"已加载
" : "未加载
";

另外,phpinfo() 显示 mongodb 扩展已经加载.

<小时>

更新:我的问题还没有解决.

phpinfo() 清楚地显示驱动已加载:

但我仍然收到同样的致命错误.

解决方案

TL;DR

MongoClient 类是旧版 PECL 包 mongo 的一部分但不再是最新的 mongodb 包.

并且由于您安装了 mongodb 扩展程序,而不是 mongo 扩展程序,这就是您收到错误的原因

<块引用>

致命错误:找不到MongoClient"类

在 MongoDB PHP driver github repo 上,关于 1.0.0 版本的发布说明,建议开发人员使用 MongoDBDriverManager 而不是 MongoClient

<块引用>

对我们旧版 mongo 扩展程序的更改

最重要的是,遗留驱动程序的 MongoClient、MongoDB 和MongoCollection 类已被废弃MongoDBDriverManager 类,这是连接的新网关并执行查询、命令和写入操作.

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

所以,这里是替换类文档和应该替换你的代码片段:

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


由于文档的提示,该类已被弃用.

<块引用>

警告 定义此类的此扩展已弃用.反而,应该使用 MongoDB 扩展.这个类的替代品包括:

来源:http://php.net/MongoClient


根据我在他们的 github 存储库发布历史记录中阅读的内容,您尝试使用的类自 mongodb 1.0.0 版本以来已过时,因此,在您使用的版本 1.6.0 上,此类是甚至不再是 dll 的一部分.

他们 github 上的这个问题证实了这一点

<块引用>

derickr 于 4 月 16 日发表评论

MongoClient 是一个来自旧遗留的类驱动程序,不应该在这个中可用.新司机有MongoDBDriverManager,并且附带的库有MongoDB客户端.

您要么需要安装旧的遗留扩展(pecl installmongo) 并使用 PHP 5.x,或更新您的代码以使用此新驱动程序的作为旧驱动程序的类不适用于 PHP 7.有一个升级指南在http://mongodb.github.io/mongo-php-library/upgrade-指南/

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


另一种方法,正如上面引用的 MongoDB 成员所建议的那样是使用这个 pecl 扩展:https://pecl.php.net/package/mongo 而不是 https://pecl.php.net/package/mongodb 但也请注意那里的警告:

<块引用>

此软件包已被取代,但仍会针对错误和安全修复进行维护.

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
" : "not loaded
";

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


UPDATE: my problem is still not solved.

phpinfo() clearly shows that the driver is loaded:

But I am still receiving the same fatal error.

解决方案

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 MongoDBDriverManager 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 MongoDBDriverManager 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 MongoDBDriverManager("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 MongoDBDriverManager, and, the accompanying library has MongoDBClient.

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.

这篇关于MongoDB:致命错误:找不到“MongoClient"类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆