Php 对Composer导入类的命名空间中断调用

Php 对Composer导入类的命名空间中断调用,php,namespaces,composer-php,Php,Namespaces,Composer Php,我目前正在尝试将我通过Composer创建的一些类自动加载到一些旧代码中 我当前的文件夹结构非常简单,如下所示: { "require": { "guzzlehttp/guzzle": "^6.2", "adoy/oauth2": "^1.3", "twig/twig": "^1.30" }, "autoload": { "psr-4": {"ApiReddit\\": "src/"}

我目前正在尝试将我通过Composer创建的一些类自动加载到一些旧代码中

我当前的文件夹结构非常简单,如下所示:

{
    "require": {
            "guzzlehttp/guzzle": "^6.2",
            "adoy/oauth2": "^1.3",
            "twig/twig": "^1.30"
    },
    "autoload": {
      "psr-4": {"ApiReddit\\": "src/"}         
    }
/src

  • Searcher.php

  • Authenticator.php

composer.json

index.php

我有更多的文件,但这些才是最重要的。然后,我将以下内容添加到我的两个类中:

namespace ApiReddit;
我的composer.json如下所示:

{
    "require": {
            "guzzlehttp/guzzle": "^6.2",
            "adoy/oauth2": "^1.3",
            "twig/twig": "^1.30"
    },
    "autoload": {
      "psr-4": {"ApiReddit\\": "src/"}         
    }
问题是,现在我以前工作的Searcher.php文件出现了一些问题。在代码执行过程中,我有以下调用:

$client = new GuzzleHttp\Client([
       'headers' => ['User-Agent' => 'testing/1.0'],
       'verify' => false]);
}
现在会生成以下错误:

致命错误:在中找不到类“ApiReddit\GuzzleHttp\Client”


这意味着新名称空间的使用打破了我对其他composer加载包的调用。你知道如何解决这个问题吗?

你可以做以下两件事之一:

在搜索程序文件中导入类时,要么显式

use GuzzleHttp\Client;

class Searcher {

...

}
或在直接调用时转义类:

$client = new \GuzzleHttp\Client([
       'headers' => ['User-Agent' => 'testing/1.0'],
       'verify' => false]);
}

您可以执行以下两种操作之一:

在搜索程序文件中导入类时,要么显式

use GuzzleHttp\Client;

class Searcher {

...

}
或在直接调用时转义类:

$client = new \GuzzleHttp\Client([
       'headers' => ['User-Agent' => 'testing/1.0'],
       'verify' => false]);
}

看起来问题在于您使用的是相对命名空间。尝试将:
new-GuzzleHttp\Client
更改为
new-GuzzleHttp\Client
。问题应该解决。

看起来问题在于您使用的是相对命名空间。尝试将:
new-GuzzleHttp\Client
更改为
new-GuzzleHttp\Client
。这个问题应该解决