WHat's new in PHP 8.2

MergePHP December 2022

Ian Littman

@ian@phpc.social / @iansltx

https://ian.im/php82merge

Thanks to release managers Ben Ramsey, Pierrick Charron, and Sergey Panteleev

What we'll cover

  • Where to get it now
  • What's new
  • What's deprecated/removed
  • How to find out more information

How do I get it?

New Features

Before

class Post
{

    public readonly string $title;
    public readonly Status $status;

    // snip
}

After

readonly class Post
{

    public string $title;
    public Status $status;

    // snip
}

This also disables dynamic properties and untyped properties (use "mixed")

Type improvements

  • Disjunctive Normal Form (DNF) types
    • (A&B)|C -> Must be both A and B, or C; builds on:
      • A&B -> Must implement/extend/be both A and B (PHP 8.1)
      • A|C -> Must be either A or C (PHP 8.0)
    • Parentheses required when OR'ing AND'd types
    • One layer of nesting; (A&(B|C)) is not valid
    • Only intersections in parentheses: A&(B|D) is not valid
  • New standalone types
  • random_* functions, plus e.g. rand/srand/mt_rand, now live in this extension
  • New Random\Randomizer class
    • Takes a Random\Engine impl (default "Secure")
    • nextInt(): int
    • getInt(int $min, int $max): int
    • getBytes(int $length): string
    • shuffleArray(array $array): array
    • shuffleBytes(string $bytes): string
    • pickArrayKeys(array $array, int $num): array
    • __serialize()/__unserialize()
namespace Random;

interface Engine {
    public function generate(): string;
}

interface CryptoSafeEngine extends Engine {}
  • Non-crypto-safe, seedable Engines

    • Mt19937 (Mersenne Twister, from mt_rand)

    • PcgOneseq128XslRr64 (Permutated congruential generator)
    • Xoshiro256StarStar (Xoshiro); fastest randomizer
  • Secure (same method as random_int/bytes uses; only CryptoSafe engine)
  • Implement your own (for mocks, plz no rolling your own crypto)
  • Cannot be referenced directly
  • Cannot collide with already-declared constants in class
    • Can be redeclared in the class using the trait, but must be compatible
      • Same visibility
      • Same value
      • Same finality (final consts are new in PHP 8.1)
    • Can import multiple traits with the same constants
      as long as they're compatible
function login(
    string $user,
    #[\SensitiveParameter] string $password
) {
    // …
    throw new Exception('Error');
}
 
login('root', 'root');

 

Fatal error: Uncaught Exception: Error in login.php:8
Stack trace:
#0 login.php(11): login('root', Object(SensitiveParameterValue))
#1 {main}
  thrown in login.php on line 8
enum A: string 
{
    case B = 'B';
    
    const C = [self::B->value => self::B];
}

 

Useful primarily for array keys mapping to enum names/values

minor tweaks

BC Breaks and Deprecations

  • Deprecation -> fatal in PHP 9.0
  • Can still use __set/__get
  • Can still use \stdClass
  • Can opt out of warning via #[AllowDynamicProperties] attribute
    • Attribute is automatically inherited

Function deprecations

Other Deprecations

Other Changes

Further Reading

Thanks! Questions?

What's New in PHP 8.2 - MergePHP December 2022

By Ian Littman

What's New in PHP 8.2 - MergePHP December 2022

  • 488