2008-09-21 17:14 阅读 95


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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    doujia7779 doujia7779 2008-09-21 17:28

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

    点赞 评论 复制链接分享
  • dongliuzhuan1219 dongliuzhuan1219 2008-09-21 17:23

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

      $connection = oci_connect('username', 'password', 'table');
      if (!$connection) {
        // no OCI connection.
    点赞 评论 复制链接分享
  • douji4948 douji4948 2008-09-21 19:51

    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:

    点赞 评论 复制链接分享
  • duanjuduo4573 duanjuduo4573 2008-09-21 20:29

    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:

    // 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.

    点赞 评论 复制链接分享