php7 mongodb驱动程序没有找到ObjectId

时间:2016-11-05 03:56:19

标签: php mongodb

我按照mongodb/mongo-php-library官方存储库

上的说明操作
$ pecl install mongodb
$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

但是当我尝试用phpunit对这个构造函数执行测试时:

    use MongoDB\BSON\ObjectID;

    (...)

    /**
     * X constructor.
     *
     * @param ObjectID|null $id
     */
    public function __construct(ObjectID $id = null)
    {
        if (!$id) {
            $id = new ObjectID();
        }

        $this->id = $id;
    }

    (...)

我收到了这个错误

1) tests\X\X\XTest::testBeConstructed
Error: Class 'MongoDB\BSON\ObjectID' not found

修改

如果我用这一行代码创建一个测试文件:

<?php

echo new \MongoDB\BSON\ObjectID();

我用

执行它
php test.php

它显示:

581ddc9c6fb3720352664f11

第二次编辑

根据评论中的建议,我在phpunit测试的第一行放了一些调试行:

var_dump(extension_loaded('mongodb'));
echo phpversion().PHP_EOL;
print_r(get_loaded_extensions());

这是输出

bool(true)
7.0.8-0ubuntu0.16.04.3
Array
(
    [0] => Core
    [1] => date
    [2] => libxml
    [3] => openssl
    [4] => pcre
    [5] => zlib
    [6] => filter
    [7] => hash
    [8] => pcntl
    [9] => Reflection
    [10] => SPL
    [11] => session
    [12] => standard
    [13] => phpdbg
    [14] => mysqlnd
    [15] => PDO
    [16] => xml
    [17] => bcmath
    [18] => calendar
    [19] => ctype
    [20] => curl
    [21] => dom
    [22] => mbstring
    [23] => fileinfo
    [24] => ftp
    [25] => gd
    [26] => gettext
    [27] => iconv
    [28] => intl
    [29] => json
    [30] => exif
    [31] => mcrypt
    [32] => mysqli
    [33] => pdo_mysql
    [34] => Phar
    [35] => posix
    [36] => readline
    [37] => shmop
    [38] => SimpleXML
    [39] => sockets
    [40] => sysvmsg
    [41] => sysvsem
    [42] => sysvshm
    [43] => tokenizer
    [44] => wddx
    [45] => xmlreader
    [46] => xmlwriter
    [47] => xsl
    [48] => Zend OPcache
)
PHPUnit 5.6.2 by Sebastian Bergmann and contributors.

使用以下命令运行phpunit

phpdbg -qrr /root/.composer/vendor/bin/phpunit

为什么phpunit没有识别mongodb ext?

我缺少什么?

由于

注意:如果你想知道为什么im root是因为它在docker容器中

1 个答案:

答案 0 :(得分:0)

这是一个非常新手的问题,我编译并安装mongodb扩展后安装phpdbg,所以对于phpdbg安装,没有加载mongodb扩展。我启用它并且它可以工作。