Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Namespaces in PHP 5.2 If Statement

Tags:

namespaces

php

Is it possible to use some sort of check before adding namespaces to a script that might be run on a php 5.2 server?

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

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 am sure I can get this to work without namespaces, but just wondering if there is anyway to use them within an IF statement.

like image 359
Mike Avatar asked May 26 '26 04:05

Mike


1 Answers

You could tuck the Doctrine code into a separate PHP file and require() it inside the else if branch.

like image 144
Adam Maras Avatar answered May 28 '26 19:05

Adam Maras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!