PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » PHP » PHP_Compat » Manual

Provides missing functionality for older versions of PHP.

Introduction

Introduction – Purpose and simple usage example

Description

PHP_Compat provides missing functionality in the form of functions and constants for older versions of PHP.

The replicated functions are designed to be interchangable with their native equivilants. They have the same signature, same return values and throw the same errors. Each function is unit tested to ensure accuracy.

Example

Loading a component with PHP_Compat:

<?php
require_once 'PHP/Compat.php';

PHP_Compat::loadFunction('file_get_contents');
?>

Or, if you don't wish to use the class, you can load it manually.

Loading a component manually:

<?php
require_once 'PHP/Compat/Function/file_put_contents.php';
?>

The function is then ready to use like you would normally.

PHP_Compat is designed for ease of use. It has no dependencies and can be used completely outside the PEAR infrastructure.

Components

Components – List of replicated components

Functions

The following functions have been replicated:

Function Since
array_change_key_case() PHP 4.2.0
array_chunk() PHP 4.2.0
array_combine() PHP 5.0.0
array_diff_assoc() PHP 4.3.0
array_diff_key() PHP 5.0.2
array_diff_ukey() PHP 5.0.2
array_intersect_assoc() PHP 5.0.0
array_intersect_key() PHP 5.0.2
array_intersect_uassoc() PHP 5.0.0
array_intersect_ukey() PHP 5.0.2
array_key_exists() PHP 4.1.0
array_product() PHP 5.1.0
array_search() PHP 4.0.5
array_udiff() PHP 5.0.0
array_udiff_assoc() PHP 5.0.0
array_udiff_uassoc() PHP 5.0.0
array_uintersect() PHP 5.0.0
array_uintersect_assoc() PHP 5.0.0
array_uintersect_uassoc() PHP 5.0.0
array_walk_recursive() PHP 5.0.0
call_user_func_array() PHP 4.0.4
bcinvert() PHP 5.2.0
bcpowmod() PHP 5.0.0
clone() PHP 5.0.0
constant() PHP 4.0.4
convert_uudecode() PHP 5.0.0
convert_uuencode() PHP 5.0.0
debug_print_backtrace() PHP 5.0.0
file_get_contents() PHP 4.3.0
file_put_contents() PHP 5.0.0
floatval() PHP 4.2.0
fprintf() PHP 5.0.0
fputcsv() PHP 5.0.0
get_headers() PHP 5.0.0
get_include_path() PHP 4.3.0
html_entity_decode() PHP 4.3.0
htmlspecialchars_decode() PHP 5.1.0
http_build_query() PHP 5.0.0
ibase_timefmt() PHP 5.0.0
idate() PHP 5.1.0
image_type_to_mime_type() PHP 4.3.0
inet_ntop() PHP 5.1.0
inet_pton() PHP 5.1.0
ini_get_all() PHP 4.2.0
is_a() PHP 4.2.0
is_scalar() PHP 4.0.5
md5_file() PHP 4.2.0
mhash() PHP 4.1.0
mime_content_type() PHP 4.3.0
ob_clean() PHP 4.2.0
ob_flush() PHP 4.2.0
ob_get_clean() PHP 4.3.0
ob_get_flush() PHP 4.3.0
php_strip_whitespace() PHP 5.0.0
property_exists() PHP 5.1.0
pg_affected_rows() PHP 4.2.0
pg_escape_bytea() PHP 4.2.0
pg_unescape_bytea() PHP 4.2.0
restore_include_path() PHP 4.3.0
scandir() PHP 5.0.0
set_include_path() PHP 4.3.0
str_ireplace() PHP 5.0.0
str_rot13() PHP 4.2.0
str_shuffle() PHP 4.3.0
str_split() PHP 5.0.0
str_word_count() PHP 4.3.0
stripos() PHP 5.0.0
strpbrk() PHP 5.0.0
strripos() PHP 5.0.0
substr_compare() PHP 5.0.0
time_sleep_until() PHP 5.1.0
var_export() PHP 4.2.0
version_compare() PHP 4.1.0
vprintf() PHP 4.1.0
vsprintf() PHP 4.1.0

Constants

The following constants have been replicated:

File Constants Since
DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR PHP 4.0.6
E_STRICT
  • E_STRICT
PHP 5
FILE
  • FILE_USE_INCLUDE_PATH
  • FILE_IGNORE_NEW_LINES
  • FILE_SKIP_EMPTY_LINES
  • FILE_APPEND
  • FILE_NO_DEFAULT_CONTEXT
PHP 5
PATH_SEPARATOR
  • PATH_SEPARATOR
PHP 4.3.0
PHP_EOL
  • PHP_EOL
PHP 5
STD
  • STDIN
  • STDOUT
  • STDERR
PHP 4.3.0
T
  • T_ML_COMMENT
  • T_DOC_COMMENT
  • T_OLD_FUNCTION
  • T_ABSTRACT
  • T_CATCH
  • T_FINAL
  • T_INSTANCEOF
  • T_PRIVATE
  • T_PROTECTED
  • T_PUBLIC
  • T_THROW
  • T_TRY
  • T_CLONE
PHP 5
UPLOAD_ERR
  • UPLOAD_ERR_OK
  • UPLOAD_ERR_INI_SIZE
  • UPLOAD_ERR_FORM_SIZE
  • UPLOAD_ERR_PARTIAL
  • UPLOAD_ERR_NO_FILE
PHP 4.3.0

Advanced usage

Advanced usage – Describe techniques for advanced usage

Advanced usage

In order to create portable code, it's best to assume all the native functions already exist. Littering a script with calls to PHP_Compat can become messy, and increase the work required to port the code.

To solve this there are a number of approaches. The first would be adding the load statements to a file which is included at the top of every document. The second, and preferred method is to make use of php_auto_append php.ini value. This can be set in a .htaccess file, and no modifications to the actual code are required.

If phpcompat.php was the name of the file, an example entry in a .htaccess could be php_value php_auto_append phpcompat.php. The phpcompat.php file would make use of either the loadVersion or loadFunction/loadConstant methods discussed in the next page.

PHP_Compat::loadFunction

PHP_Compat::loadFunction() – Load a function, or an array of functions

Synopsis

require_once 'PHP/Compat.php';

mixed PHP_Compat::loadFunction ( mixed $function )

Description

Loads a function, or an array of functions.

Parameter

mixed $function

The name, or an array of names, of functions to load

Return value

mixed

Example

Loading a function with the class:

<?php
require_once 'PHP/Compat.php';

// load file_put_contents
PHP_Compat::loadFunction('file_put_contents');

// load str_split, array_chunk and file_get_contents
PHP_Compat::loadFunction(array('str_split', 'array_chunk', 'file_get_contents'));
?>

You may also load a function without using the class.

Loading a function manually:

<?php
require_once 'PHP/Compat/Function/file_put_contents.php';
?>

Note

This function should be called statically.

PHP_Compat::loadConstant

PHP_Compat::loadConstant() – Load a constant, or an array of constants

Synopsis

require_once 'PHP/Compat.php';

mixed PHP_Compat::loadConstant ( mixed $constant )

Description

Loads a constant, or an array of constants.

Parameter

mixed $constant

The name, or an array of names, of constants to load

Return value

mixed

Example

Loading a constant with the class:

<?php
require_once 'PHP/Compat.php';

// load E_STRICT
PHP_Compat::loadConstant('E_STRICT');

// load E_STRICT, and PATH_SEPATATOR
PHP_Compat::loadConstant(array('E_STRICT', 'PATH_SEPARATOR'));

// load the STD group of constants (STDIN, STDOUT, STDERR)
PHP_Compat::loadConstant('STD');
?>

You may also load a constant without using the class.

Loading a constant manually:

<?php
require_once 'PHP/Compat/Constant/E_STRICT.php';
?>

Note

This function should be called statically.

PHP_Compat::loadVersion

PHP_Compat::loadVersion() – Load all components, or all components until a given version of PHP

Synopsis

require_once 'PHP/Compat.php';

array PHP_Compat::loadVersion ( string $version )

Description

Load all components, or all components until a given version of PHP.

Parameter

string $version

The version of PHP to load components until.

Return value

array

Example

Loading all components:

<?php
require_once 'PHP/Compat.php';
$components = PHP_Compat::loadVersion();

// To see which components were loaded
print_r($components);
?>

This example would show a long list of components which were loaded.

Loading up to a specific version:

The components loaded would be those with versions lower than, or equal to the supplied version and greater than the current PHP version.

<?php
require_once 'PHP/Compat.php';
$components = PHP_Compat::loadVersion('4.3.0');

// To see which components were loaded
print_r($components);
?>

This would output an array of components which were loaded. The output would be simular to:


Array
(
    [array_diff_assoc] => 1
    [file_get_contents] => 1
    [get_include_path] => 1
    [html_entity_decode] => 1
    [image_type_to_mime_type] => 1
    [ob_get_clean] => 1
    [ob_get_flush] => 1
    [restore_include_path] => 1
    [set_include_path] => 1
    [str_shuffle] => 1
    [str_word_count] => 1
    [FILE] => 1
    [STD] => 1
    [UPLOAD_ERR] => 1
)

Note

This function should be called statically.