Using namespaces in PHP 5.2. If the expression

Is there any way to check before adding namespaces to the script that can be run on php 5.2 server?

For example, if you want to use the doctrine (which requires 5.3) on server 5.3 and fall back to the PDO on server 5.2.

Example:

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

}

This is just an example, I'm sure I can make it work without namespaces, but it's just interesting if there is any use for them in the IF statement.

+3
source share
2 answers

You can put the Doctrine code in a separate PHP file and require()inside the branch else if.

+5
source

Another solution:

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

Not tested.

0
source

Source: https://habr.com/ru/post/1789529/


All Articles