2011-02-02 35 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(); 

雖然未測試。