Topics Modern PHP (8.0 - 8.4) PHP 8.2 - 8.3 Features
advanced 12 min read

PHP 8.2 - 8.3 Features

Readonly classes, disjunctive normal form types, randomizer, json_validate, and more.

PHP 8.2 Features

Readonly Classes

readonly class ValueObject {
public function __construct(
public string \$name,
public int \$value,
) { }
}

Disjunctive Normal Form Types

// Intersection types combined with union types
function process(
(Countable&ArrayAccess)|string \$input
): void { }

Randomizer Extension

\$random = new \Random\Randomizer();
\$num = \$random->getInt(1, 100);
\$bytes = \$random->getBytes(16);
\$shuffled = \$random->shuffleArray(\$items);

PHP 8.3 Features

json_validate()

if (json_validate(\$jsonString)) {
\$data = json_decode(\$jsonString);
}

Override Attribute

class Child extends Parent {
#[\\Override]
public function method(): void { ... }
}

Dynamic class constant fetch

echo \$className::\$constantName;

Examples

<?php
readonly class Point {
    public function __construct(
        public float $x,<br>        public float $y,<br>    ) { }
}
$p = new Point(3.0, 4.0);
echo $p->x; // 3

if (json_validate('[1,2,3]')) {
    echo 'Valid JSON';
}

Your Notes

Sign in to take notes for this lesson.

Discussion

Sign in to join the discussion.

Flashcards

Sign in to create flashcards.