DEV Community

Cover image for PHP 8.1 On New Heights With Amazing New Additions 🎉
Sakis bal
Sakis bal

Posted on

PHP 8.1 On New Heights With Amazing New Additions 🎉

PHP Improvements

PHP is on a roll lately and spoiling us with some amazing additions. On PHP 8.0 there were additions like Union Types which helped us set more complex static types, JIT with big performance improvements, named arguments and these are just a few.

Now PHP is adding Enums, Array unpacking with string keys, and Fibers driving the language to new heights.

What are those enums and why should i care?

To those who aren't aware enums (enumerations) are used when a variable is supposed to take a specific range of values. Until now if you wanted to create an enum-like type in PHP you would have to directly use static variables or associative arrays like below:

class Car
{
    static $carState = [
        "RUNNING" => "RUNNING",
        "STOPPED" => "STOPPED"
    ];

    private float $_speed = 0;
    private string $_name;
    private string $_state;

    public function __construct(float $speed, string $name, string $carState)
    {
        $this->_name = $name;
        $this->_speed = $speed;
        $this->_state = $carState;
    }
}
Enter fullscreen mode Exit fullscreen mode

Here we could use the state to determine which types of cars states we have. Most code editors would type-hint the types when we pressed Car::$carState.

Do you see the issue with that? The class has no idea if you actually used the types that we have selected! A solution to this would be to simply manually check in the constructors or in setters if the variable that is passed has the appropriate values. But you probably agree that this sucks, and can lead to more bugs than we should have to handle.

Enums are here to solve this issue.

<?php

enum CarTypes {
    case running;
    case stopped;
}

class Car
{
    static $carState = [
        "RUNNING" => "RUNNING",
        "STOPPED" => "STOPPED"
    ];

    private float $_speed = 0;
    private string $_name;
    private CarTypes $_state;

    public function __construct(float $speed, string $name, CarTypes $carState)
    {
        $this->_name = $name;
        $this->_speed = $speed;
        $this->_state = $carState;
    }
}
Enter fullscreen mode Exit fullscreen mode

by using this new built-in data type we can automatically skip all the checks and let enum do it for us. Neat!

What about fibers?

Fibers are utilizing 'green threads' to create virtual parallelism. While the computer is busy doing work in controllers or fetching data from database fibers helps regulate the traffic between those actions.

According to the RFC:

The Fiber API is not expected to be used directly in >application-level code. Fibers provide a basic, low-level >flow-control API to create higher-level abstractions that >are then used in application code"

This means that you will probably not use Fibers in your production code but will rather use them with the help of frameworks that utilize them like ReactPHP.

to see all changes of PHP 8.1 visit the following website
https://stitcher.io/blog/new-in-php-81

Top comments (0)