未找到 PHP Mongo DB 类 'MongoDBCollection'


PHP Mongo DB Class 'MongoDBCollection' not found

我正在使用 http://docs.php.net/manual/en/mongodb.tutorial.library.php,当我使用他们提供的示例时,我得到了一个PHP Fatal error: Class 'MongoDB'Collection' not found.我正在使用作曲家,即使没有我得到的require mongodb'mongodbMongoDB'Driver'Manager Object,如果我打印$manager表明它正在使用一个MongoDB,它不是我安装的那个(因为它没有安装,至少通过作曲家)。

有什么想法可以找到MongoDB或解决我的问题吗?

解决:因为我是从命令行而不是从 Luracast/Restler 使用它的,所以我需要在命令行期间要求在构造上自动加载

namespace NES'Utils;
use MongoDB;
class Mongo
{
    private $manager;
    private $collection;
    public $result;
    function __construct($collection)
    {
        require_once "../../vendor/autoload.php";
        $this->manager = new MongoDB'Driver'Manager("mongodb://localhost:27017");
        $this->collection = new MongoDB'Collection($this->manager,"nestories.queue");
        $result = $this->collection->find( [ 'teste' => 'teste'] );
        print_r($result);
        }
}

问候

您是否在项目中使用作曲家自动加载?另外,您是否在使用命名空间?使用 Composer mongo 软件包的另一种方法是为 Mongo 安装 PECL 扩展:https://pecl.php.net/package/mongo

如果使用 PECL 扩展,则需要确保它已安装在每个环境中。

遇到的错误很可能是因为您没有正确包含供应商自动加载.php文件。

<?php
require 'vendor/autoload.php';

当您尝试包含文件时,您的错误日志输出会说什么?如果您使用的是 xdebug,它将转储大量内容,但通常您应该收到无法包含文件"/路径/到/不正确/文件/名称.php"错误。