Skip to content

InfinitumForm/php-legacy-compat

Repository files navigation

PHP Legacy Compat

PHP 8 Legacy Compatibility Layer (PHP 5.x / 7.x to 8.3 Migration Toolkit)

Production-ready PHP compatibility files for developers upgrading old PHP applications to PHP 8.0, PHP 8.1, PHP 8.2, and PHP 8.3.

This project helps fix real migration breakage caused by removed functions, deprecated behavior, and stricter runtime rules in modern PHP. It is designed for legacy WordPress themes and plugins, old Laravel applications, Symfony legacy bundles, CodeIgniter 2 and 3 projects, Magento 1 and legacy Magento codebases, and custom PHP frameworks or enterprise systems that still depend on PHP 5.x and PHP 7.x behavior.

What This Project Solves

Upgrading old PHP code to PHP 8 often causes immediate fatal errors and behavioral regressions:

  • create_function() removed
  • each() removed
  • ereg(), eregi(), ereg_replace(), eregi_replace() removed
  • split() and spliti() removed
  • utf8_encode() and utf8_decode() removed
  • strftime() and gmstrftime() deprecated
  • count(null) and similar loose calls now fail or throw
  • old implode() argument order no longer works
  • old array, string, regex, and session assumptions break under PHP 8 strictness

This repository provides:

  • removed function polyfills where behavior can be reproduced honestly
  • documented approximations where full restoration is impossible
  • safe migration helpers for stricter PHP 8 behavior
  • two compatibility modes for different risk tolerance levels

Two Modes

Universal

php-legacy-compat-universal.php is the broader compatibility layer.

Use it when:

  • you are migrating a difficult legacy application
  • the priority is keeping old code operational on PHP 8
  • you need broader compatibility coverage for old plugins, themes, packages, or custom systems

The universal build includes practical approximations where that is still honest and useful, such as broader migration helpers and a documented money_format() polyfill.

Strict

php-legacy-compat-strict.php is the conservative compatibility layer.

Use it when:

  • you want safer and more controlled migration behavior
  • you prefer fewer approximations
  • you want compatibility help without enabling looser edge-case behavior

The strict build omits behavior that is too approximate or too risky for a conservative default, such as money_format(), and tightens helpers like legacy_safe_count(), legacy_safe_implode(), legacy_safe_array_merge(), and legacy_safe_array_key_exists().

Features

  • PHP 8 compatibility layer for legacy applications
  • Legacy PHP support for PHP 5.x and PHP 7.x code running on PHP 8
  • WordPress compatibility for old themes, plugins, and mu-plugins
  • Laravel legacy support for older apps and packages
  • Symfony legacy support for older bundles and utility code
  • CodeIgniter compatibility for CodeIgniter 2 and CodeIgniter 3 projects
  • Magento and Magento 1 compatibility support for legacy integrations
  • Removed function polyfills for classic migration failures
  • Deprecated function replacements and migration helpers
  • Safe PHP 8 migration helpers for string, array, regex, and counting behavior
  • Optional diagnostics and optional soft error handling for staged migrations

Supported Removed and Deprecated Areas

This project covers realistic compatibility for removed and deprecated behavior including:

  • create_function()
  • each()
  • split()
  • spliti()
  • sql_regcase()
  • ereg()
  • eregi()
  • ereg_replace()
  • eregi_replace()
  • session_register()
  • session_unregister()
  • session_is_registered()
  • utf8_encode()
  • utf8_decode()
  • magic quotes reporting fallbacks
  • strftime() and gmstrftime() replacement helpers
  • PHP 8 migration helpers such as legacy_safe_count(), legacy_safe_implode(), legacy_safe_explode(), and legacy_safe_preg_replace()

Installation

Manual Include

Choose one file and include it as early as possible in your application bootstrap.

require_once __DIR__ . '/php-legacy-compat-universal.php';

Or:

require_once __DIR__ . '/php-legacy-compat-strict.php';

WordPress mu-plugins

For WordPress, place the chosen file in wp-content/mu-plugins/ or require it from a small loader file that runs before older themes and plugins.

Application Bootstrap

Examples:

  • public/index.php
  • bootstrap/app.php
  • config/bootstrap.php
  • a front controller used by a custom framework

Composer

Install the package:

composer require infinitumform/php-legacy-compat

Then include exactly one compatibility file in your bootstrap:

require_once __DIR__ . '/vendor/infinitumform/php-legacy-compat/php-legacy-compat-universal.php';

Or:

require_once __DIR__ . '/vendor/infinitumform/php-legacy-compat/php-legacy-compat-strict.php';

Why Composer Does Not Auto-Load Both Files

The two compatibility files are intentionally mutually exclusive:

  • php-legacy-compat-universal.php
  • php-legacy-compat-strict.php

Auto-loading both through Composer autoload.files would be the wrong design because it would attempt to load two different compatibility policies in the same process. For that reason, this package stays manual-include by design after Composer installation.

That is the correct behavior for:

  • WordPress mu-plugins
  • Laravel bootstrap files
  • Symfony entry points
  • CodeIgniter front controllers
  • Magento bootstraps
  • custom PHP framework loaders

Usage

Universal Mode

Use the universal file for hard migrations and old applications that must keep running while you modernize the codebase incrementally.

require_once __DIR__ . '/php-legacy-compat-universal.php';

Strict Mode

Use the strict file when you want compatibility help but still want tighter and more predictable behavior.

require_once __DIR__ . '/php-legacy-compat-strict.php';

Diagnostics

$report = legacy_compat_get_report();
print_r($report);

Optional Soft Error Handler

The error handler is opt-in only.

legacy_compat_register_soft_error_handler();

Real Migration Problems This Fixes

create_function() Fatal Error

Old callback code:

$callback = create_function('$value', 'return trim($value);');

Modern PHP:

  • fatal error because create_function() is gone

This project restores a generated callable name string so old callback registration patterns can continue to work.

each() Undefined Function

Old code:

while (list($key, $value) = each($data)) {
    // ...
}

Modern PHP:

  • fatal error because each() was removed

This project restores the classic array-pointer behavior.

ereg() Removed

Old code:

if (ereg('^[0-9]+$', $value)) {
    // ...
}

Modern PHP:

  • fatal error because POSIX regex functions were removed

This project provides documented PCRE-backed approximations for the old POSIX regex family.

count(null) and Similar PHP 8 Failures

Old code:

if (count($items)) {
    // ...
}

Modern PHP:

  • warnings or TypeError depending on the input and PHP version

This project provides helpers like:

if (legacy_safe_count($items)) {
    // ...
}

Old implode() Argument Order

Old code:

echo implode($array, ',');

Modern PHP:

  • invalid in PHP 8

This project provides:

echo legacy_safe_implode($array, ',');

Warnings and Philosophy

This repository follows an honest compatibility philosophy:

  • no fake success values
  • no fake extension recreation
  • no fake magic quotes engine state
  • no silent data corruption disguised as compatibility
  • no stub that just returns false when realistic behavior can actually be recreated

If something cannot be reproduced exactly in userland, the limitation is documented clearly.

Release and Versioning Policy

This repository is intended for GitHub Releases and Packagist distribution.

First Stable Release

The first stable release is:

  • v1.0.0

Semantic Versioning

This project follows semantic versioning:

  • patch releases for documentation fixes, metadata cleanup, non-breaking compatibility fixes, and safe internal corrections
  • minor releases for new helpers, new non-breaking polyfills, new documentation, and expanded compatibility coverage that does not break existing consumers
  • major releases for breaking behavior changes, removed APIs, renamed files, or compatibility-policy changes that can affect existing bootstrap logic

Stable Tags and Release Policy

  • create a Git tag for each release
  • publish matching GitHub Releases
  • keep CHANGELOG.md updated for every tagged version
  • prefer stable tags for Packagist indexing and Composer consumers

Changelog

See CHANGELOG.md for release history.

Compatibility Table

Legacy Code Origin Runtime Target Status
PHP 5.x PHP 8.0 to 8.3 Supported through compatibility layers and migration helpers
PHP 7.x PHP 8.0 to 8.3 Supported through removed-function handling and migration helpers
PHP 8.0 PHP 8.1 to 8.3 Supported with deprecation-aware helpers
PHP 8.1 PHP 8.2 to 8.3 Supported with deprecated function replacements
PHP 8.2 PHP 8.3 Supported with migration helpers and polyfills where needed

Supported Frameworks and Ecosystems

This project is not limited to WordPress.

It is especially useful for:

  • WordPress legacy themes and plugins
  • Laravel legacy apps and old packages
  • Symfony legacy bundles and internal libraries
  • CodeIgniter 2 and CodeIgniter 3 applications
  • Magento 1 and legacy Magento integrations
  • Custom PHP frameworks
  • Legacy enterprise PHP systems
  • Older internal tools, CRMs, admin panels, and proprietary stacks

Repository Structure

php-legacy-compat-universal.php
php-legacy-compat-strict.php
README.md
LICENSE
CONTRIBUTING.md
CHANGELOG.md
composer.json
docs/

Documentation

Author

Author: Ivijan-Stefan Stipić
LinkedIn: https://www.linkedin.com/in/ivijanstefanstipic/

Senior Full-Stack Developer focused on performance, systems architecture, and large-scale PHP applications.

License

This project is licensed under the MIT License. See LICENSE.

Keywords

  • php 8 compatibility
  • php 8 deprecated functions
  • php create_function removed
  • php each removed
  • php ereg alternative
  • php migration tool
  • wordpress php 8 fix
  • legacy php support
  • php upgrade issues

About

PHP 8 legacy compatibility layer to fix removed and deprecated functions (create_function, each, ereg, etc.) in WordPress, Laravel, Symfony and legacy PHP applications.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages