PHP Annotated – March 2020

php_annotated

Greetings everyone,

We’re pleased to present the March edition of PHP Annotated. It includes 4 new RFCs from PHP Internals, including Attributes v2 and a PHP language evolution proposal. It also covers Laravel 7, CodeIgniter 4, and other releases, as well as articles on Laravel and Symfony, useful tools, videos, podcasts, and plenty of other exciting posts!

⚡️ News & Releases

  • CodeIgniter 4.0 – After 5 years of development, a new version of the framework has been released. It’s been rewritten from scratch, and it works on PHP 7.2+ and implements PSR-1, 3, and 4.
  • PHP 7.4.3 – This release has a long-awaited fix for the opcache.preload_user bug, and preloading can now be used in production

🐘 PHP Internals

  • Accepted proposals: [RFC] Variable Syntax Tweaks, [RFC] Static return type, [RFC] Allow ::class on objects.
  • [RFC] Stringable – This proposal from Nicolas Grekas has also been accepted. In PHP 8, it will be possible to use the string|Stringable union type in places where a string is expected, and it will be possible to pass a class in them with the __toString() method.

    Interestingly, there’s no need to add implements Stringable explicitly, as this will be implemented automatically behind the scenes for all classes with __toString().

  • Language evolution – Nikita has started a discussion about how to keep PHP moving forward without breaking legacy code. One option is to introduce “editions,” similar to what Rust has, and declare them using per-file declares (for example, declare(edition=2020)).
  • [RFC] Write-Once Properties – This is a proposal to add a new property modifier that will allow you to initialize specific properties but disable their modification. There are several options for the name of the modifier: final, immutable, readonly, writeonce, locked, sealed.
    class Foo
    {
        <keyword> public int $a = 1;
        <keyword> public string $b;
        <keyword> public array $c = ["foo"];
        <keyword> public object $d;
    
        public function __construct()
        {
            $this->b = "foo";
        }
    }
    
    $foo = new Foo();
    
    $foo->a = 42;       // EXCEPTION: property a has already been initialized
    $foo->b = "bar";    // EXCEPTION: property b has already been initialized
    $foo->a++;          // EXCEPTION: incrementing/decrementing is forbidden
    unset($foo->c);     // EXCEPTION: unsetting is forbidden
    $foo->c[] = "bar";  // EXCEPTION: arrays can't be modified
    $var= &$this->c;    // EXCEPTION: reference isn't allowed
    
    $foo->d = new Foo();    // SUCCESS: property d hasn't been initialized before
    $foo->d->foo = "foo";   // SUCCESS: objects are still mutable internally
    
  • [RFC] Allow explicit call-site pass-by-reference annotation – This updated RFC suggests making it possible to explicitly indicate the passing of arguments by reference. This will warn a developer that the passed variable is going to change. It also proposes adding a directive to make the mode optional.
    declare(require_explicit_send_by_ref=1);
    
    function byRef(&$ref) {...}
    byRef(&$var);
    
  • [RFC] Increment/Decrement Fixes – In some cases, incremental and decremental operators do not behave in the same way that the explicit addition and subtraction of 1 with an assignment do. For example:
    <?php
    
    $a = [];
    $a = ++$a; // [] and no errors
    $a = $a + 1; // Fatal error
    

    It is proposed to fix inconsistencies in PHP 8 and throw an Error where appropriate.

  • [RFC] Attributes v2 – This is a second attempt to add full annotations in PHP with double bracket syntax <<...>>. There have been previous proposals for simple annotations and attributes, though the latter did not pass the voting.
    use Doctrine\ORM\Mapping as ORM;
    
    <<ORM\Entity(["repositoryClass" => UserRepository::class])>>
    <<ORM\Table("users")>>
    class User
    {
        <<ORM\Id, ORM\Column, ORM\GeneratedValue>>
        public int $id;
    
        <<ORM\Column(["unique" => true])>>
        public string $email;
    
        <<ORM\ManyToOne()>>
        public ?Address $address;
    }
    
  • [PR] Make sorting stable – The standard sorting functions in PHP are not stable. This means that the preservation of the original order of elements with the same values is not guaranteed. Here’s an example. There’s been a suggestion to fix this and make the sorting stable. However, with a large number of identical elements, such a fix would affect performance.

🛠 Tools

Symfony

Laravel

Yii

Zend/Laminas

🌀 Async PHP

💡 Misc

📺 Videos

🔈 Podcasts

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