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

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

问题描述

在执行以下PHP代码时:

When executing the following PHP code:

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

我收到以下错误:

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

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

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

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

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

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";

此外,phpinfo()显示mongodb扩展名已加载.

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

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

UPDATE: my problem is still not solved.

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

phpinfo() clearly shows that the driver is loaded:

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

But I am still receiving the same fatal error.

推荐答案

TL; DR

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

TL; DR

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

在MongoDB PHP驱动程序github repo上,有关版本1.0.0的发行说明建议开发人员使用MongoDB\Driver\Manager而不是MongoClient

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

对我们的传统mongo扩展的更改

最重要的是,旧驱动程序的MongoClient,MongoDB和 MongoCollection类已被 MongoDB \ Driver \ Manager类,它是用于连接的新网关 并执行查询,命令和写入操作.

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.

来源: 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.

警告,不建议使用定义此类的扩展名.反而, 应该使用 MongoDB 扩展名.本课程的替代品 包括:

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

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

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.

此问题在其github上得到确认

derickr于4月16日发表评论

derickr commented on Apr 16

MongoClient是旧版的类 驱动程序,不应在此驱动程序中使用.新司机 具有\ MongoDB \ Driver \ Manager,并且随附的库具有 \ MongoDB \ Client.

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.

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

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/

来源: 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 ,但也请注意以下警告:

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.

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

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