PEAR logo

PHP_CompatInfo : The Definitive Guide

Chapter 6. Basic detection

Table of Contents

Detection of a single file
Detection of a directory

Detection of a single file

In most case, the basic detection is enough. But sometimes, we will need to adjust accuracy of parser to give the best result. It is possible with $option, the second parameter of each parser method. See parser options list for details.

Suppose we have to detect which PHP version we need to run this script named "math.php"

  1. <?php
  2. $nb = bcsub(1.234, 5, 4);
  3. if (preg_match('/^-/', $nb)) {
  4.     echo 'minus';
  5. }
  6. ?>

We will use this very simple detection script.

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

Default output used the Array renderer (we will talk about it and other renderers later; don't be afraid if you don't know what is it yet). Here are the raw results we got on standard output :

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

It means that we need at least PHP 4.0.0 to run the "math.php" script. with two PHP extensions bcmath, pcre loaded.

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