Top Related Projects
Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
PHP 5.x support for random_bytes() and random_int()
Compatibility with the password_* functions that ship with PHP 5.5
Quick Overview
symfony/polyfill-php72 is a PHP library that provides polyfills for PHP 7.2 features to be used in earlier PHP versions. It allows developers to use PHP 7.2 functions and classes in projects running on PHP 5.3 to 7.1, enhancing compatibility and enabling the use of newer language features in older environments.
Pros
- Enables the use of PHP 7.2 features in older PHP versions
- Improves code portability across different PHP versions
- Simplifies the transition process when upgrading PHP versions
- Maintained by the Symfony community, ensuring reliability and updates
Cons
- May introduce a slight performance overhead compared to native implementations
- Requires including an additional library in your project
- Not a complete replacement for upgrading to PHP 7.2 or later
- Some polyfills may have limitations or slight differences compared to native implementations
Code Examples
- Using the
spl_object_id
function:
use Symfony\Polyfill\Php72\Php72;
$object = new stdClass();
$id = spl_object_id($object);
echo "Object ID: $id";
- Using the
stream_isatty
function:
use Symfony\Polyfill\Php72\Php72;
if (stream_isatty(STDOUT)) {
echo "STDOUT is a TTY\n";
} else {
echo "STDOUT is not a TTY\n";
}
- Using the
mb_ord
function:
use Symfony\Polyfill\Php72\Php72;
$char = 'A';
$unicode = mb_ord($char);
echo "Unicode code point of '$char': $unicode";
Getting Started
To use symfony/polyfill-php72 in your project, follow these steps:
-
Install the library using Composer:
composer require symfony/polyfill-php72
-
Include the Composer autoloader in your PHP script:
require_once 'vendor/autoload.php';
-
Use the polyfilled functions and classes as if you were using PHP 7.2:
$result = stream_isatty(STDIN); $codePoint = mb_ord('ñ');
The polyfills will be automatically used when running on PHP versions earlier than 7.2, providing a seamless experience across different PHP versions.
Competitor Comparisons
Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
Pros of polyfill-php73
- Supports newer PHP 7.3 features, including
is_countable()
andarray_key_first()
- Provides compatibility for projects using PHP 7.3 syntax on older PHP versions
- Includes all functionality from polyfill-php72, offering a more comprehensive solution
Cons of polyfill-php73
- Slightly larger file size due to additional features
- May introduce unnecessary overhead if only PHP 7.2 compatibility is required
- Potential for conflicts if used alongside polyfill-php72 in the same project
Code Comparison
polyfill-php72:
if (!function_exists('is_iterable')) {
function is_iterable($var) {
return is_array($var) || $var instanceof Traversable;
}
}
polyfill-php73:
if (!function_exists('is_countable')) {
function is_countable($var) {
return is_array($var) || $var instanceof Countable;
}
}
Both polyfills provide similar functionality, but polyfill-php73 includes additional features specific to PHP 7.3. The choice between the two depends on the required PHP version compatibility and the specific features needed in a project. polyfill-php73 is generally more comprehensive, but polyfill-php72 may be sufficient for projects not utilizing PHP 7.3 features.
Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
Pros of polyfill-php80
- Supports more recent PHP 8.0 features, including new functions and classes
- Provides compatibility for newer language constructs like union types
- Includes polyfills for additional functions like
fdiv()
andget_debug_type()
Cons of polyfill-php80
- May have a slightly larger footprint due to supporting more features
- Potentially less stable or mature compared to polyfill-php72
- Could introduce unnecessary overhead if targeting PHP versions below 8.0
Code Comparison
polyfill-php80:
if (!function_exists('fdiv')) {
function fdiv($dividend, $divisor) {
return @($dividend / $divisor);
}
}
polyfill-php72:
if (!function_exists('spl_object_id')) {
function spl_object_id($object) {
return Symfony\Polyfill\Php72\Php72::spl_object_id($object);
}
}
The code examples show how each polyfill implements missing functions for their respective PHP versions. polyfill-php80 adds newer functions like fdiv()
, while polyfill-php72 focuses on older functions like spl_object_id()
.
PHP 5.x support for random_bytes() and random_int()
Pros of random_compat
- Focused specifically on providing secure random number generation
- Extensively audited for security vulnerabilities
- Supports a wider range of PHP versions (5.2 to 7.0+)
Cons of random_compat
- Limited in scope to random number generation only
- May require additional polyfills for other PHP 7.0+ features
- Slightly larger file size due to comprehensive random number support
Code Comparison
random_compat:
$bytes = random_bytes(32);
$integer = random_int(0, 100);
polyfill-php72:
$bytes = random_bytes(32);
$integer = random_int(0, 100);
// Also includes other PHP 7.2+ polyfills
Summary
While both libraries provide polyfills for PHP features, random_compat focuses solely on secure random number generation, offering extensive security audits and wider PHP version support. polyfill-php72, on the other hand, provides a broader range of PHP 7.2+ features but with less depth in any specific area. The code usage for random functions is identical, but polyfill-php72 includes additional polyfills beyond random number generation.
Compatibility with the password_* functions that ship with PHP 5.5
Pros of password_compat
- Focused specifically on password hashing functionality
- Lightweight and easy to integrate into existing projects
- Provides a straightforward API for password hashing and verification
Cons of password_compat
- Limited scope compared to polyfill-php72's broader PHP 7.2 feature set
- Less actively maintained (last update in 2018)
- May not be necessary for newer PHP versions that include native password hashing functions
Code Comparison
password_compat:
$hash = password_hash("password123", PASSWORD_DEFAULT);
$verify = password_verify("password123", $hash);
polyfill-php72:
$str = mb_chr(0x1F600);
$result = spl_object_id($object);
$stream = stream_isatty(STDOUT);
While password_compat focuses solely on password hashing functions, polyfill-php72 provides a broader range of PHP 7.2 features. The code examples demonstrate this difference in scope, with password_compat offering specific password-related functions and polyfill-php72 covering various PHP 7.2 functionalities.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Symfony Polyfill / Php72
This component provides functions added to PHP 7.2 core:
And also functions added to PHP 7.2 mbstring:
On Windows only:
Moved to core since 7.2 (was in the optional XML extension earlier):
Also, it provides constants added to PHP 7.2:
More information can be found in the main Polyfill README.
License
This library is released under the MIT license.
Top Related Projects
Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
PHP 5.x support for random_bytes() and random_int()
Compatibility with the password_* functions that ship with PHP 5.5
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot