PEAR logo

PHP_CompatInfo : The Definitive Guide

Chapter 6. Advanced detection

Table of Contents

Detection of a single file
Usage with SAPI
Usage with CLI
Detection of files into a directory
Usage with SAPI
Usage with CLI

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.

Usage with SAPI

Suppose we have to detect which PHP version we need to run this chunk of script named "errorHandler.php". With standard behavior, PHP_CompatInfo 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. $info = new PHP_CompatInfo();
  5. $path_to_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'errorHandler.php';
  6. $options = array('ignore_functions' => array('debug_backtrace'));
  7. $res = $info->parseFile($path_to_file, $options);
  8.  
  9. echo '<pre>'; var_dump($res); echo '</pre>';
  10. ?>

And the raw results are :

array(4) {
  ["max_version"]=>
  string(0) ""
  ["version"]=>
  string(5) "3.0.7"
  ["extensions"]=>
  array(0) {
  }
  ["constants"]=>
  array(0) {
  }
}
     

Usage with CLI

pci command used files to specify list of values on --ignore- switchs.

pci -f \wamp\www\pci\errorHandler.php -in \wamp\www\pci\functions.txt
     
[Note] Note
functions.txt is a simple text file with one line by PHP function that should be ignore. In our example, content is one line with value :

debug_backtrace

And result give:

+------------------------+---------+------------+------------------+
| File                   | Version | Extensions | Constants/Tokens |
+------------------------+---------+------------+------------------+
| [...]\errorHandler.php | 3.0.7   |            |                  |
+------------------------+---------+------------+------------------+
     
PHP_CompatInfo : The Definitive Guide v 1.5.1 : November 19, 2007