2011-02-02 6 views
3

php 5.2サーバーで実行される可能性のあるスクリプトに名前空間を追加する前に、何らかのチェックを使用できますか?PHP 5.2で名前空間を使用する場合ステートメント

たとえば、5.3サーバーでdoctrine(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文の中でそれらを使用するとにかくがある場合だけ不思議。

答えて

5

Doctrineコードを別のPHPファイルに入れて、else ifブランチ内にあるrequire()とすることができます。

+0

これは最高の賭けのように見えます...ありがとう – Mike

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

けれどもテストされていません。

関連する問題