PHP 8.1 Syntax Showcase

PHP 8.1 was released when I was suffering from COVID so I'm a bit late.

<?php

enum Status
{
    case OK;
    case Error;
}

#[SomeAttr(new SomeNestedAttr())]
class TestTest
{
    public function __construct(
        public readonly Status $status = Status::OK,
        private Logger $logger = new FileLogger(perm: 0o644),
    ) {}

    public function process(Iterator&Countable $value)
    {
        if (count($value) === 0) {
            $this->error();
        }
    }

    public function error(): never
    {
        $this->logger->error('Error!');
        exit;
    }
}

$test = new TestTest();

$arr1 = ['a' => new ArrayObject()];
$arr2 = ['b' => new ArrayIterator()];

$result = array_map($test->process(...), [...$arr1, ...$arr2]);

Comments

Comments powered by Disqus