在PHP5.2IF语句中使用名称空间

在PHP5.2IF语句中使用名称空间,php,namespaces,Php,Namespaces,在向可能在PHP5.2服务器上运行的脚本添加名称空间之前,是否可以使用某种检查 例如,如果您希望在5.3服务器上使用条令(需要5.3),并在5.2服务器上回退到PDO 例如: if($pdo){ //RETURN a pdo connection } else if($doctrine){ //this will fail even if doctrine is false because namespaces are being used $classLoader = n

在向可能在PHP5.2服务器上运行的脚本添加名称空间之前,是否可以使用某种检查

例如,如果您希望在5.3服务器上使用条令(需要5.3),并在5.2服务器上回退到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语句中是否有使用它们的方法。

您可以将条令代码塞入一个单独的PHP文件中,并
require()
将其放入
中,否则如果
分支。

另一个解决方案是:

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

但未经测试。

看来这是最好的选择。。。谢谢