質問

Is there any way to determine whether FirePHP is installed on the server (via PEAR)? I'd like to make possible logging in FirePHP but also not to crash the code for everyone without that tool.

The example, how I imagine it should work:

$message = "hello";
function log($message) {
    if (library_exists('FirePHPCore/fb.php')) {
        require_once('FirePHPCore/fb.php');
        ob_start();
        \FB::log($message);
    } else {
        SomeBoringLogger::log($message);
    }
}

I haven't found anything like my library_exists method. Is there anything like that in PHP?

役に立ちましたか?

解決

@include_once('FirePHPCore/fb.php'); // Ignore any errors here, as we check for existance
if (class_exists('FirePHP')) { // Do something after this

http://php.net/manual/en/function.class-exists.php

FirePHP uses FirePHP as its class name, so if it is available, that class should be defined


For PHP 5.3.2 or later, use zerkms's suggestion:

(!stream_resolve_include_path('FirePHPCore/fb.php')===FALSE)

他のヒント

Use include_once, so it doesn't kill the request. As @Brad suggests, use class_exists afterwards.

$message = "hello";

safe_include_once('FirePHPCore/fb.php');
if (class_exists('FB')) {
   function log($message) { 
      //using FirePHP
   }
} else {
   function log($message) {
      SomeBoringLogger::log($message);
   }
}

function safe_include_once($path) {
  if ($path = stream_resolve_include_path($path)) {
    include_once($path);
  }
}

[Edit] Using stream_resolve_include_path in safe_include_path.

[Edit2] Faster runtime logging.

file_exists() can be used for your case.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top