• Reading time ~ 2 min
  • 10.12.2022

The PHP team has released PHP 8.2 today with read-only classes, new stand-alone types, a new random extension, trait constants, and more:

Readonly classes

Building on PHP 8.1's read-only properties, marking a class as read-only makes every property on a class read-only and prevents the creation of dynamic properties.

readonly class BlogData
{
    public string $title;
 
    public Status $status;
 
    public function __construct(string $title, Status $status)
    {
        $this->title = $title;
        $this->status = $status;
    }
}

Disjunctive normal form types (DNF)

DNF types combine union and intersection types :

class Foo {
    public function bar((A&B)|null $entity) {
        if ($entity === null) {
            return null;
        }
 
        return $entity;
    }
}

Null, false, and true stand-alone types

PHP 8.2 now allows false, true, and null as standalone types. This example is pretty self-explanatory:

class Falsy
{
    public function alwaysFalse(): false { /* ... */ *}
 
    public function alwaysTrue(): true { /* ... */ *}
 
    public function alwaysNull(): null { /* ... */ *}
}

Constants in traits

Constants are now allowed in traits. You cannot access constants through the name of the trait, however, you can access the constant through the class using the trait:

trait Foo
{
    public const CONSTANT = 1;
 
    public function bar(): int
    {
        return self::CONSTANT; // Fatal error
    }
}
 
class Bar
{
    use Foo;
}
 
var_dump(Bar::CONSTANT); // 1

Dynamic property deprecation

Dynamic properties is deprecated, meaning that you will get a deprecation notice when assigning a value to dynamic property:

class User
{
    public $name;
}
 
$user = new User();
$user->last_name = 'Doe'; // Deprecated notice
 
$user = new stdClass();
$user->last_name = 'Doe'; // Still allowed

You also have the option to allow dynamic properties using the AllowDynamicProperties attribute:

#[AllowDynamicProperties]
class User() {}
 
$user = new User();
$user->foo = 'bar';

New classes, interfaces, attributes, and functions

PHP 8.2 contains new classes, interfaces, attributes, and functions. For the complete list, check out the New Classes, Interfaces, and Functions section of the PHP 8.2.0 Release Announcement.

We've already mentioned the AllowDynamicProperties attribute. Another attribute is the #[\SensitiveParameter] attribute, which redacts sensitive information within a stack trace:

function sensitiveParametersWithAttribute(
    #[\SensitiveParameter]
    string $secret,
    string $normal
) {
    throw new Exception('Error!');
}

Learn more

To get up to speed on these new features, check out the PHP 8.2.0 Release Announcement page for examples before/after PHP 8.2, and view these links for more details on each item:

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297