检查PHP代码中是否安装了FirePHP


Check whether FirePHP is installed in PHP code

是否有任何方法可以确定服务器上是否安装了FirePHP(通过PEAR)?我想让登录FirePHP成为可能,但也不想让没有该工具的每个人都崩溃代码。

例如,我认为它应该如何工作:

$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);
    }
}

我还没有找到任何类似于我的library_exists方法的东西。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使用FirePHP作为类名,因此如果它可用,则应定义该类的


对于PHP 5.3.2或更高版本,请使用zerkms的建议:

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

使用include_once,这样就不会终止请求。正如@Brad建议的那样,之后使用class_exists

$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);
  }
}

[编辑]在safe_include_path中使用stream_resolve_include_path

[Edit2]更快的运行时日志记录。

file_exists()可以用于您的案例。