PEAR logo

PHP_CompatInfo : The Definitive Guide

Chapter 7. Advanced detection

Table of Contents

Detection of a single file
Detection of a directory

Detection of a single file

If your file implement code condition that is optional and don't break main goal, such as, for example : if function_exists then I do something, else I do something else.

Solution is very easy: You have to specify what function required should be considered as optional.

Suppose we have to detect which PHP version we need to run this chunk of script named "errorHandler.php". With standard behavior, PCI returns PHP 4.3.0 (because debug_backtrace came with version 4.3.0). So, if we ignore function debug_backtrace to find out the minimum version, we will get the real and true result.

  1. <?php
  2. // ...
  3. if (function_exists('debug_backtrace')) {
  4.     $backtrace = debug_backtrace();
  5. } else {
  6.     $backtrace = false;
  7. }
  8. // ...
  9. ?>

We will use another very simple detection script. Have a look on options array given as second parameter (here is the magic).

  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $source  = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'errorHandler.php';
  5. $options = array('ignore_functions' => array('debug_backtrace'));
  6.  
  7. $info = new PHP_CompatInfo();
  8. $info->parseFile($source, $options);
  9. // you may also use unified method:  $info->parseData($source, $options);
  10. ?>

And displayed results are :

array (
  'ignored_files' =>
  array (
  ),
  'ignored_functions' =>
  array (
    0 => 'debug_backtrace',
  ),
  'ignored_extensions' =>
  array (
  ),
  'ignored_constants' =>
  array (
  ),
  'max_version' => '',
  'version' => '4.0.0',
  'extensions' =>
  array (
  ),
  'constants' =>
  array (
  ),
  'tokens' =>
  array (
  ),
  'cond_code' =>
  array (
    0 => 1,
  ),
)
    

that means chunk of script named "errorHandler.php" need PHP 4.0.0, have condition code (function condition : cond_code = 1), and php debug_backtrace function was excluded from scope.

[Note] Note

Since version 1.7.0, you may catch this situation (more easily), and exclude from scope all functions that are conditionned by a function_exists. See example that follow.

Other alternative is to use ignore_functions_match option.

  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $source  = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'errorHandler.php';
  5. $options = array('ignore_functions_match' => array('function_exists', array('/.*/')));
  6.  
  7. $info = new PHP_CompatInfo();
  8. $info->parseFile($source, $options);
  9. // you may also use unified method:  $info->parseData($source, $options);
  10. ?>

The string function_exists as first parameter, tell to ignore function(s) that match only conditionnal code with php function_exists().

The other possibility is string preg_match, that give more freedom, and catch function that match the pattern (without condition).

While array as second parameter, gave a list of pattern (function name) that must be catch and ignored.

PHP_CompatInfo : The Definitive Guide v 1.8.0 : August 1, 2008