News

PHP Annotated – October 2024

PHP Annotated

Welcome to the October edition of PHP Annotated! This recap is carefully handcrafted and brings you the most interesting developments in the PHP community over the past couple of months, so you don’t have to sift through the noise—we’ve done it for you.

Highlights

PHP Core

  • 📣 RFC: Support Closures in constant expressions

    This one has some interesting use-cases:

    final class Locale
    {
        #[Validator\Custom(static function (string $languageCode): bool {
            return \preg_match('/^[a-z][a-z]$/', $languageCode);
        })]
        public string $languageCode;
    }
  • 📊 RFC: Add persistent curl share handles

    Eric Norris proposes to add a new function curl_share_init_persistent(), which would allow cURL handles to be stored in global memory and reused in subsequent requests. This enhancement aims to improve performance by reducing the overhead of initializing cURL handles each time.

    Quoting Matthieu Napoli, the author of Bref, here:

    Most PHP apps spend most of their time doing IO (not using the CPU). … But calling APIs (via HTTP) can take a very long time. Very different orders of magnitude.

    Why? Because it establishes a TPC and HTTPS connection every time. That can take 100ms or 200ms (HTTPS is bonkers). On *every* request, because PHP (with FPM) recreates a connection every time.

    That’s why switching to Laravel Octane or Symfony Runtime makes such a difference sometimes: by keeping the PHP process alive between requests, we can save these 100ms. Comparing that to what we save on the framework boot time, this is massive.

    The RFC proposes to share HTTP connections between requests in PHP-FPM: that’s huge because it could bring some of these gains to all PHP apps out there, without having to switch to Octane/Runtime (and the downsides that come with it).

    And to further prove the point, here are a couple of real-world use cases and workarounds:

  • 📣 RFC: Change behaviour of array sort functions to return a copy of the sorted array

    Currently, PHP’s array sort functions take the array to sort by reference, modifying the original array in place. However, these functions always return `true` which often makes them not so convenient to use.

    Gina Banyard proposes changing the return value of these functions sort(), rsort(), asort(), arsort(), ksort(), krsort(), natsort(), natcasesort(), usort(), uasort(), uksort(), array_multisort(), shuffle(), array_walk(), array_walk_recursive() from true to a copy of the array.

    This change would allow for a more functional programming style, enabling to chain these functions and work with immutable data more easily.

  • RFC: Change Directory class to behave like an opaque object
  • 📣 RFC: Warn on conversions from resource to string

Tools

  • Composer 2.8.0
    The new version of Composer comes with several interesting additions:

    • --patch-only flag: This allows you to limit updates to patch-level changes, minimizing the risk of introducing breaking changes.
    • Ability to explicitly decide if additional arguments/options should be passed to the underlying command.
  • imliam/cpx – It’s like `npx` but for PHP. This tool allows you to run any command from any Composer package, even if it’s not installed in your project.
  • php.new – A one-line PHP installer from Laravel and Herd teams. It installs a static version of PHP intended specifically for development and testing purposes, along with composer and Laravel. Please note that it’s not suitable for production use.
  • Thavarshan/fetch-php – A lightweight package for making HTTP requests inspired by JavaScript’s fetch.
  • API Platform 4 Released – API Platform 4 is now out and officially supports Laravel. It’s a mature framework for building REST and GraphQL API-first applications.
  • Pest v3 released – The new version introduces mutation testing, arch presets, and more to streamline your testing workflow.
  • Ucar-Solutions/uri-signer – A minimal package for securely signing PSR-15 URIs in PHP applications.
  • Nejcc/php-datatypes – Flexible yet strict way of handling primitive data types like integers, floats, and strings in PHP.
  • tempestphp/tempest-framework v1.0.0-alpha – Brent tagged the first alpha version of his framework that is designed so developers can write as little framework-related code as possible and focus on app logic.
  • cerbero90/json-parser – A zero-dependency lazy parser to read JSON of any size and from any source in a memory-efficient way.
  • beyondcode/php-spx – A simple and straightforward PHP profiling extension with a built-in web UI. It’s a fork of a popular profiler with a brand-new UI.
  • shipmonk-rnd/dead-code-detector – A PHPStan extension for detecting unused code in your PHP projects.
  • shipmonk-rnd/phpstan-baseline-per-identifier – A PHPStan error formatter that generates baseline files for each error identifier.
  • pxp-lang/trunk – An all-in-one tool for managing and developing PHP projects. It’s implemented in Rust but installs just like any Composer package.
  • staabm/side-effects-detector – Analyzes PHP code for side effects to help maintain clean codebases.
  • yiisoft/mailer – A flexible yet simple mailer component.
  • phikiphp/phiki – Syntax highlighting powered by TextMate grammars.
  • cerbero90/enum – Supercharges enums in your PHP projects.
  • mario-deluna/php-glfw – A fully-featured OpenGL and GLFW extension for PHP.

    It introduces a whole new set of tools for PHP developers to build graphical applications like games, scientific simulations, and user interfaces.

    Check out some examples:

AI

Frameworks

Misc

Conferences

These PHP events are all worth a visit, and some are still accepting presentation proposals:

To find a PHP meetup happening near you, check out the calendar on php.net.

Fun

  • Fizz-Buzz in 56 characters?

    On the code-golf.io site, you can compete to implement the shortest solutions to simple programming problems. For example, try writing the shortest FizzBuzz code in PHP — currently at just 56 characters!

    Well, my naive solution counts 72 chars. Can you do better?

    for(;$i++<100;)echo$i%15?($i%3?($i%5?$i:'Buzz'):'Fizz'):'FizzBuzz',"\n";

If you have any interesting or useful links to share via PHP Annotated, please leave a comment on this post or let us know on X/Twitter.

Subscribe to PHP Annotated

Roman Pronskiy

Developer Advocate at @PhpStorm, Executive Director at @The PHP Foundation.

Twitter | GitHub

image description