2011-02-02 89 views
3

在将命名空间添加到可能在PHP 5.2服务器上运行的脚本之前是否可以使用某种检查?在PHP 5.2中使用命名空间如果声明

例如,如果要在5.3服务器上使用doctrine(需要5.3),并在5.2服务器上使用backoff to PDO。

例子:

if($pdo){ 

    //RETURN a pdo connection 

} 
else if($doctrine){ 

    //this will fail even if doctrine is false because namespaces are being used 
    $classLoader = new Doctrine\Common\ClassLoader('Doctrine\Common'); 
    $classLoader->register(); 

} 

这只是一个例子,我相信我能得到这个没有命名空间的工作,只是不知道是否有反正IF语句中使用它们。

回答

5

你可以把Doctrine代码放到一个单独的PHP文件中,require()它在else if分支中。

+0

看起来像这是最好的选择...谢谢 – Mike 2011-02-03 15:54:45

0

另一种解决方案是:

$classLoaderName = 'Doctrine\\Common\\ClassLoader'; 
//this will fail even if doctrine is false because namespaces are being used 
$classLoader = new $classLoaderName('Doctrine\Common'); 
$classLoader->register(); 

虽然未测试。