Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在使用命名空间时,无法一次加载带有require_的类_Php_Namespaces_Slim 3 - Fatal编程技术网

Php 在使用命名空间时,无法一次加载带有require_的类

Php 在使用命名空间时,无法一次加载带有require_的类,php,namespaces,slim-3,Php,Namespaces,Slim 3,我正在使用Slim开发一个简单的restapi,我面临一个奇怪的问题。基本上,我配置了API,通过composer使用autoloader加载所有类: "autoload": { "psr-4": { "App\\Controllers\\": "app/controllers", "App\\Helpers\\": "app/helpers", "App\\Models\\": "app/models", "App\\Se

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

"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
的类,该类必须包含
googleapi php库
,因此我以以下方式包括:

<?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();
    }
}
代码能够加载该类。因此我怀疑
require_once
无法注入类,因为存在自动加载逻辑,但我可能错了

此外,方法
文件\u存在于构造函数中,返回
true


发生了什么?

您在一个名称空间中调用它,因此它尝试使用该名称空间,可以尝试如下实例化它:
$this->client=new\Google\u client()
忽略名称空间


您在一个名称空间中调用它,因此它尝试使用该名称空间,也许可以尝试如下实例化:$this->client=new\Google_client();忽略名称空间。谢谢,这个问题真的很容易解决,如果你回答了我将接受的问题,祝你有个愉快的一天
Core\Libraries