PEAR logo

PHP_CompatInfo : The Definitive Guide

Name

PHP_CompatInfo::parseFile — Parse a single file

Synopsis

    require_once 'PHP/CompatInfo.php';
   
Array PHP_CompatInfo::parseFile( $file,  
  $options = array());  
string   $file;
array   $options = array();

Description

Parse a file for its compatibility info

Parameter

string $file

Path of File to parse

array $options

An array of options where:

  • debug Contains a boolean to control whether extra ouput is shown.

  • ignore_functions Contains an array of functions to ignore when calculating the version needed.

  • ignore_constants Contains an array of constants to ignore when calculating the version needed.

  • ignore_extensions Contains an array of php extensions to ignore when calculating the version needed.

  • ignore_versions Contains an array of php versions to ignore when calculating the version needed.

  • ignore_functions_match Contains an array of function patterns to ignore when calculating the version needed.

  • ignore_extensions_match Contains an array of extension patterns to ignore when calculating the version needed.

  • ignore_constants_match Contains an array of constant patterns to ignore when calculating the version needed.

Throws

throws no exceptions thrown

See

see PHP_CompatInfo::parseData

Since

since version 0.7.0 (2004-03-09)

Note

This function can not be called statically.

Return value

array - a hash which contains information keys: ignored_functions, ignored_extensions, ignored_constants, max_version, version, extensions, constants, tokens, cond_code

Example

Suppose we have to parse source code like this one, named "conditional.php" :

  1. <?php
  2. // PHP 4.0.0 : __FILE__
  3. // PHP 4.0.6 : DIRECTORY_SEPARATOR
  4. // PHP 4.0.7 : version compare
  5. // PHP 4.3.0 : ob_get_clean
  6. // PHP 4.3.0 : debug_backtrace
  7. // PHP 4.3.10 and 5.0.2 : PHP_EOL
  8. // PHP 5.0.0 : simplexml_load_file
  9. // PHP 5.1.1 : DATE_W3C
  10.  
  11. if (!defined('DIRECTORY_SEPARATOR')) {
  12.     define('DIRECTORY_SEPARATOR',
  13.         strtoupper(substr(PHP_OS, 0, 3) == 'WIN') ? '\\' : '/'
  14.     );
  15. }
  16.  
  17. if (function_exists('debug_backtrace')) {
  18.     $backtrace = debug_backtrace();
  19. } else {
  20.     $backtrace = false;
  21. }
  22.  
  23. if (function_exists('simplexml_load_file')) {
  24.     $xml = simplexml_load_file('C:\php\pear\PHP_CompatInfo\scripts\version.xml');
  25. }
  26.  
  27. if (version_compare(phpversion(), '5.0.0', '<')) {
  28.     include_once 'PHP/Compat.php';
  29.     PHP_Compat::loadFunction('ob_get_clean');
  30.     PHP_Compat::loadConstant('PHP_EOL');
  31. }
  32.  
  33. echo "Hello World" . PHP_EOL;
  34.  
  35. $ds  = DIRECTORY_SEPARATOR;
  36. $fn  = dirname(__FILE__) . $ds . basename(__FILE__);
  37. echo "You have run file : $fn at " . date(DATE_W3C) . PHP_EOL;
  38.  
  39. ?>
  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $pci = new PHP_CompatInfo();
  5.  
  6. $input   = 'conditional.php';
  7. $options = array('ignore_functions' => array('simplexml_load_file'),
  8.                  'ignore_constants' => array('DATE_W3C')
  9.                 );
  10.  
  11. $res = $pci->parseFile($input, $options);
  12.  
  13. var_export($res);
  14. ?>

We get such result.

array (
  'ignored_functions' =>
  array (
    0 => 'simplexml_load_file',
  ),
  'ignored_extensions' =>
  array (
  ),
  'ignored_constants' =>
  array (
    0 => 'DATE_W3C',
  ),
  'max_version' => '',
  'version' => '4.3.10',
  'extensions' =>
  array (
    0 => 'date',
  ),
  'constants' =>
  array (
    0 => 'PHP_EOL',
    1 => 'DIRECTORY_SEPARATOR',
    2 => '__FILE__',
    3 => 'DATE_W3C',
  ),
  'tokens' =>
  array (
  ),
  'cond_code' =>
  array (
    0 => 5,
    1 =>
    array (
    ),
  ),
)
   

That means php simple_load_file() function and constant DATE_W3C were ignored from scope, and PHP version minimum to run is 4.3.10 (in such condition)

[Warning] Warning

Of course it is impossible to run such script with only PHP 4.3.10 because usage of DATE_W3C constant is not conditionned.

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