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