PHP 8.2 Released

PHP 8.2 has been released. It's a relatively small release by new features. I already covered some of them. Here are things that I would like to highlight:

Random Extension

I already covered that in the Early Look and even wrote a polyfill.

<?php

$rng = new \Random\Engine\PcgOneseq128XslRr64(123);
$rnd = new \Random\Randomizer($rng);
var_dump($rnd->getInt(1, 1000)); // 566

// or

$rng = new \Random\Engine\Secure();
$rnd = new \Random\Randomizer($rng);
var_dump($rnd->getInt(1, 1000)); // something really random

#[SensitiveParameter]

Also covered

<?php

function connect(
    string $username,
    #[SensitiveParameter] $password,
): void {
    throw new RuntimeException();
}

connect('me', 'secret');

/*
    PHP Fatal error:  Uncaught RuntimeException in sensitive.php:5
    Stack trace:
    #0 sensitive.php(8): connect('me', Object(SensitiveParameterValue))
    #1 {main}
      thrown in sensitive.php on line 5
*/

Readonly Classes

Basically a shortcut:

<?php

class ValueObject {
    public function __construct(
        public readonly int $a,
        public readonly string $b,
        public readonly float $c,
    ) {}
}

becomes

<?php

readonly class ValueObject {
    public function __construct(
        public int $a,
        public string $b,
        public float $c,
    ) {}
}

Also as a bonus it strictly disables dynamic properties for the class too. (But they are deprecated anyway)

Disjunctive Normal Form Types

A huge change on the engine I believe but it's probably useful for <1% of all PHP based projects. I would like to see them as little as possible.

<?php

function iterate((Traversable&Countable)|array $something): mixed
{
    if (count($something)) {
        foreach ($something as $s) {
            // ...
        }
    }
}

Comments

Comments powered by Disqus