Convert Figma logo to code with AI

symfony logopolyfill

PHP polyfills

2,709
138
2,709
19

Top Related Projects

29,705

The Symfony PHP framework

PHP 5.x support for random_bytes() and random_int()

Compatibility with the password_* functions that ship with PHP 5.5

2,374

All PHP functions, rewritten to throw exceptions instead of returning false

Quick Overview

The Symfony Polyfill project is a set of PHP classes that provide functionality missing from the current version of PHP. It aims to provide a consistent set of functions and classes that can be used across different versions of PHP, allowing developers to write code that works on a wide range of PHP environments.

Pros

  • Compatibility: The Symfony Polyfill project helps ensure that your code works consistently across different versions of PHP, reducing the need for complex version-specific logic.
  • Standardization: By providing a consistent set of functions and classes, the Symfony Polyfill project helps to standardize the way developers interact with certain PHP features, making it easier to write and maintain code.
  • Extensibility: The Symfony Polyfill project is designed to be easily extended, allowing developers to add support for new PHP features as they are introduced.
  • Community Support: The Symfony Polyfill project is maintained by the Symfony community, which means that it benefits from a large and active user base, as well as regular updates and bug fixes.

Cons

  • Dependency: Using the Symfony Polyfill project means that your code will have an additional dependency, which can increase the complexity of your project's setup and deployment.
  • Performance: The Symfony Polyfill project may introduce a small amount of overhead, as it needs to provide functionality that is not natively available in the current version of PHP.
  • Maintenance: As the Symfony Polyfill project needs to keep up with changes in PHP, it may require regular updates to ensure that it continues to work as expected.
  • Scope: The Symfony Polyfill project may not cover every single feature that is missing from the current version of PHP, so developers may still need to write custom code to handle certain use cases.

Code Examples

Here are a few examples of how you can use the Symfony Polyfill project in your PHP code:

// Using the Symfony Polyfill for the `mb_str_split()` function
use Symfony\Polyfill\Mbstring\Mbstring;

$string = 'Hello, World!';
$chars = Mbstring::str_split($string, 1);
print_r($chars);

This code uses the Mbstring class from the Symfony Polyfill project to split a string into an array of individual characters, even on systems where the native mb_str_split() function is not available.

// Using the Symfony Polyfill for the `is_iterable()` function
use Symfony\Polyfill\Php70\Php70;

$data = [1, 2, 3, 4, 5];
if (Php70::is_iterable($data)) {
    foreach ($data as $item) {
        echo $item . ' ';
    }
}

This code uses the Php70 class from the Symfony Polyfill project to check if a variable is iterable, which is a feature that was introduced in PHP 7.0. This allows the code to work on older versions of PHP that do not have the native is_iterable() function.

// Using the Symfony Polyfill for the `mb_chr()` function
use Symfony\Polyfill\Mbstring\Mbstring;

$character = Mbstring::chr(0x2603);
echo $character; // Output: ☃

This code uses the Mbstring class from the Symfony Polyfill project to get the Unicode character for a given code point, which is a feature that was introduced in PHP 7.2. This allows the code to work on older versions of PHP that do not have the native mb_chr() function.

Getting Started

To use the Symfony Polyfill project in your PHP code, you can install it using Composer:

composer require symfony/polyfill

Once you have installed the Symfony Polyfill project, you can start using the provided classes and functions in your code. The specific classes and functions that you need to use will depend on the PHP features that you are trying to access.

For example, to use the mb_str_split() function, you would need to include the Symfony\Polyfill\Mbstring\Mbstring class and call the `str

Competitor Comparisons

29,705

The Symfony PHP framework

Pros of symfony

  • Comprehensive full-stack framework with extensive features and components
  • Highly modular architecture allowing for flexible usage and customization
  • Large, active community providing support and continuous development

Cons of symfony

  • Larger footprint and potentially steeper learning curve
  • May include unnecessary components for smaller projects
  • Higher resource requirements for deployment and execution

Code Comparison

symfony:

use Symfony\Component\HttpFoundation\Response;

$response = new Response();
$response->setContent('<html><body>Hello, World!</body></html>');
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type', 'text/html');

polyfill:

if (!function_exists('mb_strlen')) {
    function mb_strlen($string, $encoding = null) {
        return \Symfony\Polyfill\Mbstring\Mbstring::mb_strlen($string, $encoding);
    }
}

The symfony example showcases a more feature-rich approach to handling HTTP responses, while the polyfill example demonstrates a simple function implementation for cross-version compatibility.

symfony offers a complete framework solution, whereas polyfill focuses on providing backward compatibility for specific PHP functions across different versions.

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
  • Includes comprehensive unit tests and continuous integration

Cons of random_compat

  • Limited scope compared to Polyfill's broader feature set
  • May require additional dependencies for full functionality
  • Less frequent updates and maintenance

Code Comparison

random_compat:

$bytes = random_bytes(32);
$integer = random_int(0, 100);

Polyfill:

use Symfony\Polyfill\Php70\Php70;

$bytes = Php70::random_bytes(32);
$integer = Php70::random_int(0, 100);

Key Differences

  • Polyfill provides a wider range of PHP compatibility features
  • random_compat focuses solely on secure random number generation
  • Polyfill integrates seamlessly with Symfony framework
  • random_compat offers more detailed documentation on cryptographic security

Use Cases

  • Choose random_compat for projects requiring only secure random number generation
  • Opt for Polyfill when needing broader PHP compatibility features
  • Consider Polyfill for Symfony-based projects for better integration

Community and Support

  • Polyfill has a larger community and more frequent updates
  • random_compat benefits from security-focused maintainers and audits

Both libraries serve important roles in PHP development, with random_compat excelling in secure random number generation and Polyfill offering a comprehensive set of compatibility features.

Compatibility with the password_* functions that ship with PHP 5.5

Pros of password_compat

  • Focused specifically on password hashing compatibility
  • Lightweight and easy to integrate for projects only needing password functionality
  • Provides a straightforward implementation of the password_* functions

Cons of password_compat

  • Limited scope compared to Polyfill's broader range of PHP compatibility features
  • Less frequently updated, with the last commit being several years old
  • May not be necessary for newer PHP versions that already include these functions

Code Comparison

password_compat:

$hash = password_hash("password", PASSWORD_DEFAULT);
if (password_verify("password", $hash)) {
    echo "Password is valid!";
}

Polyfill:

use Symfony\Polyfill\Php54\Php54;

$hash = Php54::hash_pbkdf2("sha256", "password", "salt", 1000, 20);
if (hash_equals($hash, Php54::hash_pbkdf2("sha256", "password", "salt", 1000, 20))) {
    echo "Password is valid!";
}

Summary

While password_compat provides a targeted solution for password hashing compatibility, Polyfill offers a more comprehensive set of polyfills for various PHP versions. password_compat is simpler to use for projects only needing password functionality, but Polyfill is more actively maintained and covers a broader range of compatibility issues. The choice between them depends on the specific needs of your project and the PHP versions you need to support.

2,374

All PHP functions, rewritten to throw exceptions instead of returning false

Pros of Safe

  • Provides type-safe wrappers for PHP functions, enhancing code reliability
  • Throws exceptions instead of returning false or null on errors, improving error handling
  • Offers a more consistent API for PHP functions

Cons of Safe

  • Limited to wrapping existing PHP functions, not providing new functionality
  • Requires learning a new API for familiar PHP functions
  • May have a slight performance overhead due to additional function calls

Code Comparison

Safe:

use function Safe\file_get_contents;

$content = file_get_contents('file.txt');
// Throws exception on failure

Polyfill:

$content = file_get_contents('file.txt');
if ($content === false) {
    // Manual error handling required
}

Key Differences

  • Safe focuses on improving PHP's native functions, while Polyfill aims to provide compatibility layers for newer PHP features in older versions
  • Polyfill is more widely used and maintained by the Symfony community
  • Safe requires explicit use of its functions, whereas Polyfill works transparently in the background

Use Cases

  • Use Safe when prioritizing type safety and consistent error handling in your project
  • Choose Polyfill when needing to support multiple PHP versions or use newer language features in older environments

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Symfony Polyfill

This project backports features found in the latest PHP versions and provides compatibility layers for some extensions and functions. It is intended to be used when portability across PHP versions and extensions is desired.

Polyfills are provided for:

  • the apcu extension when the legacy apc extension is installed;
  • the ctype extension when PHP is compiled without ctype;
  • the mbstring and iconv extensions;
  • the uuid extension;
  • the MessageFormatter class and the msgfmt_format_message functions;
  • the Normalizer class and the grapheme_* functions;
  • the utf8_encode and utf8_decode functions from the xml extension or PHP-7.2 core;
  • the Collator, NumberFormatter, Locale and IntlDateFormatter classes, limited to the "en" locale;
  • the intl_error_name, intl_get_error_code, intl_get_error_message and intl_is_failure functions;
  • the idn_to_ascii and idn_to_utf8 functions;
  • a Binary utility class to be used when compatibility with mbstring.func_overload is required;
  • the spl_object_id and stream_isatty functions introduced in PHP 7.2;
  • the mb_ord, mb_chr and mb_scrub functions introduced in PHP 7.2 from the mbstring extension
  • the sapi_windows_vt100_support function (Windows only) introduced in PHP 7.2;
  • the PHP_FLOAT_* constant introduced in PHP 7.2;
  • the PHP_OS_FAMILY constant introduced in PHP 7.2;
  • the is_countable function introduced in PHP 7.3;
  • the array_key_first and array_key_last functions introduced in PHP 7.3;
  • the hrtime function introduced in PHP 7.3;
  • the JsonException class introduced in PHP 7.3;
  • the get_mangled_object_vars, mb_str_split and password_algos functions introduced in PHP 7.4;
  • the fdiv function introduced in PHP 8.0;
  • the get_debug_type function introduced in PHP 8.0;
  • the preg_last_error_msg function introduced in PHP 8.0;
  • the str_contains function introduced in PHP 8.0;
  • the str_starts_with and str_ends_with functions introduced in PHP 8.0;
  • the ValueError class introduced in PHP 8.0;
  • the UnhandledMatchError class introduced in PHP 8.0;
  • the FILTER_VALIDATE_BOOL constant introduced in PHP 8.0;
  • the get_resource_id function introduced in PHP 8.0;
  • the Attribute class introduced in PHP 8.0;
  • the Stringable interface introduced in PHP 8.0;
  • the PhpToken class introduced in PHP 8.0 when the tokenizer extension is enabled;
  • the array_is_list function introduced in PHP 8.1;
  • the enum_exists function introduced in PHP 8.1;
  • the MYSQLI_REFRESH_REPLICA constant introduced in PHP 8.1;
  • the ReturnTypeWillChange attribute introduced in PHP 8.1;
  • the CURLStringFile class introduced in PHP 8.1 (but only if PHP >= 7.4 is used);
  • the AllowDynamicProperties attribute introduced in PHP 8.2;
  • the SensitiveParameter attribute introduced in PHP 8.2;
  • the SensitiveParameterValue class introduced in PHP 8.2;
  • the Random\Engine interface introduced in PHP 8.2;
  • the Random\CryptoSafeEngine interface introduced in PHP 8.2;
  • the Random\Engine\Secure class introduced in PHP 8.2 (check arokettu/random-polyfill for more engines);
  • the odbc_connection_string_is_quoted function introduced in PHP 8.2;
  • the odbc_connection_string_should_quote function introduced in PHP 8.2;
  • the odbc_connection_string_quote function introduced in PHP 8.2;
  • the ini_parse_quantity function introduced in PHP 8.2;
  • the json_validate function introduced in PHP 8.3;
  • the Override attribute introduced in PHP 8.3;
  • the mb_str_pad function introduced in PHP 8.3;
  • the ldap_exop_sync function introduced in PHP 8.3;
  • the ldap_connect_wallet function introduced in PHP 8.3;
  • the stream_context_set_options function introduced in PHP 8.3;
  • the str_increment and str_decrement functions introduced in PHP 8.3;
  • the Date*Exception/Error classes introduced in PHP 8.3;
  • the SQLite3Exception class introduced in PHP 8.3;
  • the mb_ucfirst and mb_lcfirst functions introduced in PHP 8.4;
  • the array_find, array_find_key, array_any and array_all functions introduced in PHP 8.4;
  • the Deprecated attribute introduced in PHP 8.4;
  • the mb_trim, mb_ltrim and mb_rtrim functions introduced in PHP 8.4;
  • the CURL_HTTP_VERSION_3 and CURL_HTTP_VERSION_3ONLY constants introduced in PHP 8.4;

It is strongly recommended to upgrade your PHP version and/or install the missing extensions whenever possible. This polyfill should be used only when there is no better choice or when portability is a requirement.

Compatibility notes

To write portable code between PHP5 and PHP7, some care must be taken:

  • \*Error exceptions must be caught before \Exception;
  • after calling error_clear_last(), the result of $e = error_get_last() must be verified using isset($e['message'][0]) instead of null !== $e.

Usage

When using Composer to manage your dependencies, you should not require the symfony/polyfill package, but the standalone ones:

  • symfony/polyfill-apcu for using the apcu_* functions,
  • symfony/polyfill-ctype for using the ctype functions,
  • symfony/polyfill-php54 for using the PHP 5.4 functions,
  • symfony/polyfill-php55 for using the PHP 5.5 functions,
  • symfony/polyfill-php56 for using the PHP 5.6 functions,
  • symfony/polyfill-php70 for using the PHP 7.0 functions,
  • symfony/polyfill-php71 for using the PHP 7.1 functions,
  • symfony/polyfill-php72 for using the PHP 7.2 functions,
  • symfony/polyfill-php73 for using the PHP 7.3 functions,
  • symfony/polyfill-php74 for using the PHP 7.4 functions,
  • symfony/polyfill-php80 for using the PHP 8.0 functions,
  • symfony/polyfill-php81 for using the PHP 8.1 functions,
  • symfony/polyfill-php82 for using the PHP 8.2 functions,
  • symfony/polyfill-php83 for using the PHP 8.3 functions,
  • symfony/polyfill-php84 for using the PHP 8.4 functions,
  • symfony/polyfill-iconv for using the iconv functions,
  • symfony/polyfill-intl-grapheme for using the grapheme_* functions,
  • symfony/polyfill-intl-idn for using the idn_to_ascii and idn_to_utf8 functions,
  • symfony/polyfill-intl-icu for using the intl functions and classes,
  • symfony/polyfill-intl-messageformatter for using the intl messageformatter,
  • symfony/polyfill-intl-normalizer for using the intl normalizer,
  • symfony/polyfill-mbstring for using the mbstring functions,
  • symfony/polyfill-util for using the polyfill utility helpers.
  • symfony/polyfill-uuid for using the uuid_* functions,

Requiring symfony/polyfill directly would prevent Composer from sharing correctly polyfills in dependency graphs. As such, it would likely install more code than required.

Design

This package is designed for low overhead and high quality polyfilling.

It adds only a few lightweight require statements to the bootstrap process to support all polyfills. Implementations are then loaded on-demand when needed during code execution.

If your project requires a minimum PHP version it is advisable to add polyfills for lower PHP versions to the replace section of your composer.json. This removes any overhead from these polyfills as they are no longer part of your project. The same can be done for polyfills for extensions that you require.

If your project requires php 7.0, and needs the mb extension, the replace section would look something like this:

{
    "replace": {
        "symfony/polyfill-php54": "*",
        "symfony/polyfill-php55": "*",
        "symfony/polyfill-php56": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-mbstring": "*"
    }
}

Polyfills are unit-tested alongside their native implementation so that feature and behavior parity can be proven and enforced in the long run.

License

This library is released under the MIT license.