How to check the existence of a namespace in php

Maxim Seshuk picture Maxim Seshuk · Aug 20, 2013 · Viewed 23.9k times · Source

I have a php library https://github.com/tedivm/Fetch and it uses Fetch namespace and I would like to check its existence in my script.

My script code:

// 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);

but this message is always displayed. But the library is fully working.

Thanks in advance for any help!

Answer

ars265 picture ars265 · Aug 20, 2013

You need to use the entire namespace in the class_exists I believe. So something like:

class_exists('Fetch\\Server')