PHP8.2: the modern PHP
Tags: webdev, php, javascript, programming
Published: 2022-12-08 22:15:26

PHP8.2 adds a lot of new features and improvements such as readonly classes, stand-alone types(null, true, false), Disjunctive Normal Form(DNF) types, new random extension, new classes, interfaces, and functions, and also deprecations some functions and classes.

## Readonly Classes
PHP 8.1 added support for _readonly_ properties. A _readonly_ property can only be set once, and PHP actively prevents the value from being modified even from within the scope of the class.

PHP 8.2 takes _readonly_ properties a step further with readonly classes. When a class is declared as readonly, all of its properties are automatically declared readonly. Further, it prevents dynamic properties on readonly classes, and ensures that all properties are typed.

### Prevents dynamic properties on Readonly classes

```
readonly class Blog {
    public string $title;
}

$blog = new Blog();
$blog->subtitle = 'Hello';
```
The above will result in: 
`Error: Cannot create dynamic property Blog::$blog...`

### Ensures all properties are typed

```
readonly class Blog {
    public $title;
}
```
`Fatal error: Readonly property Blog::$title must have type...`

## Example

```
readonly class ExampleBlog {
    public string $myTitle;
}
```
N.B: Abstract classes and final classes can also be readonly.

## Disjunctive Normal From(DNF) Types
DNF types allow us to combine union and intersection types, following a strict rule: 

> when combining union and intersection types, intersection types must be grouped with brackets.

Previously, in PHP < 8.2:

```
class Blog {
    public function checkEntity(mixed $entity) {
        if ((($entity instanceof BlogA) && ($entity instanceof BlogB)) || ($entity === null)) {
            return $entity;
        }

        throw new Exception('Invalid entity');
    }
}
```
The above snippet in PHP8.2 with the introduction of DNF becomes:

```
class Blog {
    public function checkEntity((BlogA&BlogB)|null $entity) {
        return $entity;
    }
}
```
How beautiful is this( specially if you are lazy like me :))


## Stand-alone types( null, false, and true)
PHP 8.0 added support for Union Types, which made it possible to declare a type as a union of two or more types. If you are familiar with Typescript, this is the same concept. It allowed false and null as possible types for a Union Type, but disallowed their use as standalone types.

Typescript exampel:

```
function printId(id: number | string) {
  console.log("Your ID is: " + id);
}
```
PHP8.0 union types example:

```
class Example {
    private int|float $foo;
    public function squareAndAdd(float|int $bar): int|float {
        return $bar ** 2 + $foo;
    }
}
```
And now in PHP8.2, we can return either null, true, or false as in:

`
public function isAuthenticated(): true {
    return true;
}
`
The above function is a function that returns true.
we can also use, null and false as return type.

## Constants in Traits
Traits helps us to overcome the multiple inheritance, and now  with PHP8.2, it is possible to define constants in traits.

```
trait NodeFunctions
{
    public const CONSTANT = 1;
}

class BlogType
{
    use NodeFunctions;
}

var_dump(NodeFunctions::CONSTANT); // Error
var_dump(BlogType::CONSTANT); // 1
```
We can't access the constant through the name of the trait, but it is possible to it via the class that uses the trait.


Some of the new functions and attributes introduced are:

- New [mysqli_execute_query](https://www.php.net/manual/en/mysqli.execute-query.php) function and [mysqli::execute_query](https://www.php.net/manual/en/mysqli.execute-query.php) method.

- New [#[\AllowDynamicProperties]](https://www.php.net/manual/en/class.allow-dynamic-properties.php) and [#[\SensitiveParameter]](https://www.php.net/manual/en/class.sensitive-parameter.php) attributes.

This is just a tip of an iceberg, more can be found at the official [PHP8.2 release notes](https://www.php.net/releases/8.2/en.php).

Thanks so for reading this far, and you are welcome to put your comments down and I would really appreciate if you give me a follow.