php - 如何检查php中命名空间是否存在

标签 php class namespaces exists

我有一个php库 https://github.com/tedivm/Fetch它使用 Fetch 命名空间,我想检查它在我的脚本中是否存在。

我的脚本代码:

// Load Fetch
spl_autoload_register(function ($class) {
    $file = __DIR__ . '/vendor/' . strtr($class, '\\', '/') . '.php';
    if (file_exists($file)) {
        require $file;

        return true;
    }
});

if (!class_exists('\\Fetch')) {
  exit('Failed to load the library: Fetch');
}
$mail = new \Fetch\Server($server, $port);

但此消息始终显示。但图书馆正在全面运作。

预先感谢您的帮助!

最佳答案

我相信您需要使用 class_exists 中的整个命名空间。所以类似:

class_exists('Fetch\\Server')

关于php - 如何检查php中命名空间是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18337650/

相关文章:

php - 如何按last_visit排序并显示用户的最近访问者?

Java 构造函数错误 : Constructor is defined, 未找到

java - 尝试使用 java printwriters 附加文本文件

php - 如何在php中使用函数和变量定义命名空间?

wpf 将组合框绑定(bind)到不同命名空间中的枚举

PHP 和 MySql 转储文件中的非法排序规则混合

javascript - 验证该值不在数据库中

ruby-on-rails - 如何为两个命名空间创建路由

php - jQuery slider 值保存

C++ 嵌套模板返回类型