0

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?

Pavel S.
  • 11,892
  • 18
  • 75
  • 113
  • **Any** php library shouldn't be installed in a system shared directories. You can just copy the directory with your project and include it from *any* directory you have write access to. – zerkms Aug 28 '12 at 22:15
  • `is_file('FirePHPCore/fb.php')`? – gen_Eric Aug 28 '12 at 22:15
  • @Rocket: `is_file` doesn't respect include paths – zerkms Aug 28 '12 at 22:15
  • Search the include path. This might help: http://stackoverflow.com/questions/6041250/how-to-check-if-a-file-exists-under-include-path – EthanB Aug 28 '12 at 22:17
  • Also - it's not a good idea either. If you don't check the firephp version - it's a chance to start using obsolete and vulnerable version of it. So if you really need it - I would recommend just having a copy of it in your distribution – zerkms Aug 28 '12 at 22:19

3 Answers3

4
@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)
Brad
  • 159,648
  • 54
  • 349
  • 530
  • 2
    Add an "include_once" before that. – EthanB Aug 28 '12 at 22:19
  • And what would happen if no such file found? – zerkms Aug 28 '12 at 22:20
  • @zerkms, With the `@` out front, the PHP warning will be ignored. Unfortunately since it is not an exception, it can't be caught with `try`/`catch`. Since this is `include` and not `require`, the script won't fail. – Brad Aug 28 '12 at 22:21
  • @Brad: yep, you added `@` after my comment. PS: there are solutions without terrible `@` - http://stackoverflow.com/a/6041325/251311 – zerkms Aug 28 '12 at 22:22
  • @zerkms, Good to know! That is a much better solution. – Brad Aug 28 '12 at 22:23
2

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.

EthanB
  • 4,239
  • 1
  • 28
  • 46
0

file_exists() can be used for your case.

Ilyas Serter
  • 810
  • 1
  • 8
  • 12