PHP 8.2 in 8 code blocks

Written on 2022-10-24
readonly class PostData
{
    public function __construct(
        public string $title,
        public string $author,
        public string $body,
        public DateTimeImmutable $createdAt,
        public PostState $state,
    ) {}
}

Readonly classes


$rng = $is_production
    ? new Random\Engine\Secure()
    : new Random\Engine\Mt19937(1234);
 
$randomizer = new Random\Randomizer($rng);

$randomizer->shuffleString('foobar');

New random extension


function alwaysFalse(): false
{
    return false;
}

null, true, and false as standalone types


function generateSlug((HasTitle&HasId)|null $post) 
{ /* … */ }

Disjunctive Normal Form Types


trait Foo 
{
    public const CONSTANT = 1;
 
    public function bar(): int 
    {
        return self::CONSTANT;
    }
}

Constants in traits


function connect(
    string $user,
    #[\SensitiveParameter] string $password
) {
    // …
}

Redacted parameters


class Post {}

$post = new Post();

$post->title = 'Name';

// Deprecated: Creation of dynamic property is deprecated

Deprecated dynamic properties


enum A: string 
{
    case B = 'B';
    
    const C = [self::B->value => self::B];
}

Enum properties in const expressions


Check out my latest video: DX matters

Things I wish I knew when I started programming

Things I wish I knew when I started programming cover image

This is my newest book aimed at programmers of any skill level. This book isn't about patterns, principles, or best practices; there's actually barely any code in it. It's about the many things I've learned along the way being a professional programmer, and about the many, many mistakes I made along that way as well. It's what I wish someone would have told me years ago, and I hope it might inspire you.

Read more

Comments

Loading…
No comments yet, be the first!
Noticed a tpyo? You can submit a PR to fix it.
HomeRSSNewsletterDiscord© 2025 stitcher.io