PHP Annotated – March 2021

PHP Annotated Monthly

Greetings everyone,

In this edition of PHP Annotated, read about the latest proposals for PHP 8.1, releases of xdebug.cloud, RoadRunner 2.0, and the newly proposed PSR ClockInterface standard. And as usual, we’ve got a load of articles, tools, videos, and streams carefully selected for you.

⚡️ News

🐘 PHP Internals

  • [RFC] Enumerations
    PHP 8.1 will have long-awaited enums!

    enum RfcStatus {
        case Draft;
        case UnderDiscussion;
        case Accepted;
    }
    
    function setRfcStatus(RfcStatus $status) :void {
        // ...
    }
    
    setRFCStatus(RfcStatus::Accepted); // Ок
    setRFCStatus('Draft');             // TypeError
    

    If you like the enums feature, please consider sponsoring the authors of this RFC: Ilija Tovilo and Larry Garfield.

    Read more about enums in Brent Roose’s article and some more on php.watch.

    Symfony has already opened tickets to add enums support.

  • [RFC] Deprecate passing null to non-nullable arguments of internal functions
    In the current versions of PHP, standard functions can accept null as an argument when the parameter is not nullable without any errors.

    To fix this inconsistency, in PHP 8.1 built-in functions will throw a Deprecation notice, and in PHP 9 they will throw a TypeError. For example, str_contains("", null). 3v4l.org/OVoa0A.

  • [RFC] Array unpacking with string keys
    Unpacking arrays will work consistently in PHP 8.1, including arrays with string keys:

    $array1 = ['a' => 'apple', 'p' => 'pear'];
    $array2 = ['b' => 'banana', 'o' => 'orange'];
    $array = [...$array1, ...$array2];
    // The same as:
    $array = array_merge($array1, $array2);
    
  • [RFC] PHP\iterable\any() and all() on iterables
    The proposal to add any() and all() functions for iterators did not pass the vote.

  • [RFC] var_representation() : readable alternative to var_export()
    The idea to add an alternative for var_export also did not find support, so for now we can use a userland alternative brick/varexporter.

  • 🗳 [RFC] Fibers
    Voting on Fibers has started. In short: this is a small but important improvement to generators that endeavors to make writing asynchronous PHP code easier. For example, like this:

    The Scheduler was removed from the RFC, so the Fiber API now provides the bare minimum and is similar to the resembling feature in Ruby.

    There were some reasonable arguments against the proposal from Swoole maintainers. As well as from Joe Watkins, the author of krakjoe/parallel extension.

    Yet, fibers seem to be a real step in the direction of asynchronous capabilities, which does not contradict either Swoole or parallel.

  • 🗳 [RFC] mysqli bind in execute
    Kamil Tekiela continues the initiative to improve mysqli. This RFC proposes adding a new optional parameter to mysqli_stmt::execute() that will take an array of values and bind them automatically. Currently, you have to make a separate call to mysqli_stmt::bind_param(), which only accepts variables passed by reference.

  • [RFC] New in initializers
    In the current versions of PHP, initializers such as the default values of properties, parameters, and constants can only have constant values.

    When non-constant values are needed, properties can be initialized in a constructor, and arguments can be initialized in the method’s body. With constants, there are no such options at all.

    Nikita Popov proposes to make it possible to use objects as default values for properties, parameters, and any constants and static variables.

    static $x = new Foo();
    
    const C = new Foo();
    
    class Bar
    {
        public const CONSTANT = new Foo();
        public static Foo $staticProperty = new Foo();
    
        #[Assert\All([new Assert\Uuid()])]
        public array $uuids = [];
    
        public function __construct(
            private Logger $logger = new NullLogger()
        ) {}
    }
    
    function test($param = new Foo()) {}
    

    For now, the proposal is limited to the new operator, but the implementation allows extending support for other expressions in the future.

    An additional bonus (or an intention? 🤔) is that objects will be allowed in the attributes. That solves the problem with nested attributes.

  • [RFC] CachedIterable (rewindable, allows any key&repeating keys)
    Tyson Andre suggests adding a caching iterator. It stores the state of an iterator and internally contains immutable copies of its keys and values.

  • [RFC] Namespaces in bundled PHP extensions
    All classes and functions provided by bundled PHP extensions are currently located in the global namespace (with one exception).

    This RFC proposes a convention on using namespaces with bundled extensions. Basically, the extension name should be used as a namespace and no PHP prefix is needed. For example, OpenSSLCertificate can become OpenSSL\Certificate.

    So far, however, this only applies to new symbols added, and the migration of existing functions and classes is not affected by this RFC. But the examples show possible future transformations:
    str_contains() -> String\contains()
    in_array() -> Array\contains().

  • [RFC] noreturn type
    The authors of Psalm and PHPStan suggest adding a new type to PHP – noreturn.

    The authors of Psalm and PHPStan suggest adding a new type to PHP – noreturn. This would be a bottom type indicating that a function either always throws an exception or terminates execution, i.e. calls exit(), die(), trigger_error().

    function redirect(string $uri): noreturn {
        header('Location: ' . $uri);
        exit();
    }
    
    function redirectToLoginPage(): noreturn {
        redirect('/login');
    }
    

    There is a similar type in Hack and in Python, and it has been used in Psalm, PHPStan, and also in PhpStorm as an attribute #[NoReturn] or via an exitPoint() in .phpstormmeta.php.

phpstorm PhpStorm

🛠 Tools

Symfony

Laravel

Yii

💡 Misc

📺 Videos

🔈 Podcasts

  • php[architect] podcast #50 – Discussed the Mezzio framework, functional programming, and software dependency security.
  • PHP Ugly podcast:
    #227: – Math is Hard.
    #226: – Help Wanted, PHP Release Manager.
  • PHP Internals News podcast:
    #75 – With Nikita Popov on deprecating null, array unpacking, restrict globals usage, and phase out serializable.
    #76 – Another one with Nikita Popov about restricting the use of globals, and phasing out serializable.
    #77 – With David Gebler about the fsync() function added to PHP 8.1.
    #78 – With Andreas Heigl on moving PHP documentation to Git.

Thanks for reading!

If you have any interesting or useful links to share via PHP Annotated, please leave a comment on this post or send me a tweet.

Subscribe to PHP Annotated

Your JetBrains PhpStorm team
The Drive to Develop

image description