Max*_*huk 13 php namespaces class 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);
Run Code Online (Sandbox Code Playgroud)
但始终显示此消息.但图书馆正在全力以赴.
在此先感谢您的帮助!
ars*_*265 28
您需要使用class_exists我认为的整个命名空间.所以类似于:
class_exists('Fetch\\Server')
Run Code Online (Sandbox Code Playgroud)
正如George Steel所写,不可能检查名称空间。这是因为命名空间并不存在,而是存在的。命名空间中仅存在符号。请参见下面的示例:
namespace Foo;
class Bar {
}
var_dump(class_exists('Foo')); // bool(false)
var_dump(class_exists('Foo\Bar')); // bool(true)
Run Code Online (Sandbox Code Playgroud)
思考这个问题的一种方法是你思考姓氏的方式:不存在姓氏这样的东西,如果不检查所有人,你就无法“独立”地获得所有姓氏的列表,因此你无法直接确定一个姓氏是否是一个姓氏。姓氏存在;但它是一个人的全名 (FQN) 的一部分,因此您可以检索一个人的姓氏,或检查他们是否属于某个姓氏。
这只是一种将事物分组的方法。您可以环顾四周并编制所有“红色”事物的列表。但您无法获得所有存在颜色的列表。