tags:

views:

267

answers:

4

How can I detect, using php, if the machine has oracle (oci8 and/or pdo_oci) installed?

I'm working on a PHP project where some developers, such as myself, have it installed, but there's little need for the themers to have it. How can I write a quick function to use in the code so that my themers are able to work on the look of the site without having it crash on them?

A: 

I dont know if I fully understand your question but a simple way would be to do this:

<?php
  $connection = oci_connect('username', 'password', 'table');
  if (!$connection) {
    // no OCI connection.
  }
?>
+3  A: 

if the oci extension isn't installed, then you'll get a fatal error with farside.myopenid.com's answer, you can use function_exists('oci_connect') or extension_loaded('oci8') (or whatever the extension's actually called)

Greg
A: 

As mentioned above by Greg, programmatically you can use the function_exists() method. Don't forget you can also use the following to see all the environment specifics with your PHP install using the following:

<?php
phpinfo();
?>
Yes... but that just prints out to the screen the specifics. I want to find out what the specifics are and not print things to the screen.
John Fiala
+1  A: 

The folks here have pieces of the solution, but let's roll it all into one solution.

For just a single instance of an oracle function, testing with function___exists() is good enough; but if the code is sprinkled throughout to OCI calls, it's going to be a huge pain in the ass to wrap every one in a function_exists() test.

Therefore, I think the simplest solution would be to create a file called nodatabase.php that might look something like this:

<?php
// nodatabase.php
// explicitly override database functions with empty stubs. Only include this file
// when you want to run the code without an actual database backend. Any database-
// related functions used in the codebase must be included below.
function oci_connect($user, $password, $db = '', $charset='UTF-8', $session_mode=null)
{
}

function oci_execute($statement, $mode=0)
{
}
// and so on...

Then, conditionally include this file if a global (say, THEME_TESTING) is defined just ahead of where the database code is called. Such an include might look like this:

// define("THEME_TESTING", true) // uncomment this line to disable database usage
if( defined(THEME_TESTING) )
  include('nodatabase.php'); // override oracle API with stub functions for the artists.

Now, when you hand the project over to the artists, they simply need to make that one modification and they're good to go.

Nathan Strong
That would be ideal if I were in control of all of the code. Unfortunately, all of the directly oracle-interfacing code is maintained and updated by our clients, and that uses both oci8 and pdo_oci. So, I'm not really sure if this solution will work - not that I won't give it a try on Monday.
John Fiala
The key is that you have to be able to load the stub functions before the actual functions are actually called. If so, you should be able to make the concept work: document which OCI/PDO calls are used, write a stub function library/skeleton class, and conditionally include the class as needed.
Nathan Strong