1

我正在使用Slim开发一个简单的REST API,我面临一个奇怪的问题。基本上我配置了API通过composer使用自动加载器加载所有类:

"autoload": {
    "psr-4": {
        "App\\Controllers\\": "app/controllers",
        "App\\Helpers\\": "app/helpers",
        "App\\Models\\": "app/models",
        "App\\Services\\": "app/services",
        "Core\\": "src/core",
        "Core\\Helpers\\": "src/helpers",
        "Core\\Libraries\\": "src/libraries"
    }
}

我创建了一个GoogleSync必须包含的类Google API php library,因此我以以下方式包含:

<?php namespace Core\Libraries;

defined('BASEPATH') or exit('No direct script access allowed');

require_once __DIR__ . '/external/google-api-php-client/Google_Client.php';
require_once __DIR__ . '/external/google-api-php-client/contrib/Google_CalendarService.php';


class GoogleSync
{
    /**
     * Google API Client
     *
     * @var Google_Client
     */
    protected $client;

    public function __construct($api_settings)
    {
        var_dump(file_exists(__DIR__ . "/external/google-api-php-client/Google_Client.php"));
        $this->client = new Google_Client();
    }
}

我收到以下错误:

致命错误:未捕获错误:在 A:\Programmi\MAMP\htdocs\ci3-api\src\libraries\GoogleSync.php:44 中找不到类“Core\Libraries\Google_Client”

出于一个奇怪的原因,如果我在类Google_Client中包含以下命名空间:

Core\Libraries

代码能够加载类。所以我怀疑require_once无法注入类,因为有一个自动加载器逻辑,但我可能是错的。

此外,file_exist构造函数中的方法 return true

发生了什么?

4

1 回答 1

1

您在命名空间中调用它,因此它尝试使用该命名空间,也许尝试像这样实例化它:$this->client = new \Google_Client();忽略命名空间。

这个问题可能重复。

于 2019-08-02T08:30:06.630 回答