Parsing a simple file and display dump results

Overview

This example show how to parse a single file and detect php minimum version and extensions needed to run it.

Dependencies

This example requires mandatory resources :

Source Code

math.php : the source file php code to parse

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

The very basic script to parse the file

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

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,
  ),
)

Explains step by step

At line 6, we created a basic instance of PHP_CompatInfo without parameters. That means, we used the default Array renderer (see Output). Results are always displayed on Standard Output unless you use the Null Renderer.

Offset 0 of cond_code in results, tell us that PHP_CompatInfo has not found any conditional code (=0). So be sure that the result of version and extensions are TRUE

Finally, we can tell that math.php need PHP 4.0.0 (minimum) and bcmath + pcre extensions to run.