PHP 8.1 lays new ground on Platform.sh

Thomas di Luccio
Thomas di Luccio
DevRel Engineer
21 Dec 2021

Every year, the PHP foundation releases a new version of PHP that includes updated features for users. These new releases offer a sweet way to kick off every holiday season for developers and application makers (like me).

Just days after its official release, we made PHP 8.1 available for all projects on our Grid plans. Now, you can take advantage of the long-awaited PHP 8.1 features on Platform.sh.

Features that accelerate performance

The new PHP 8.1 features include Enums, readonly properties, first-class callable syntax, Fibers, and the use of new in initializers.

What else is new in PHP version 8.1? A feature estimated to improve performance by 5 to 8%. Dmitry Stogov, the author of this impressive feature, describes it as a new, transparent technology that eliminates the overhead of PHP class inheritance.

Additionally, as a particularly groundbreaking new feature, Fibers add a low-level mechanism to manage parallelism. You can try these features and more on Platform.sh.

Enums

Enums is a feature I’ve expected for years, and I can’t wait to use it; it’s such a convenient way to manage collections of constant values.

enum Status {
  case Draft;
  case Published;
  case Archived;
}
class Post
{
    public function __construct(
        private Status $status = Status::Draft;
        // ...
    ) {}

    public function getStatus(): Status
    {
        return $this->status;
    }

    public function setStatus(Status $status): void
    {
        $this->status = $status;
    }
}

$post->setStatus(Status::Active);

An Enum can even have methods and interfaces. How cool is that?

enum Status
{
    // …
    
    public function color(): string
    {
        return match($this) 
        {
            self::Draft => 'grey',   
            self::Published => 'green',   
            self::Archived => 'red',   
        };
    }
}
interface HasColor
{
    public function color(): string;
}

enum Status implements HasColor
{
    // ...
    
    public function color(): string { /* … */ }
}

readonly properties

With PHP 8.1, class properties can be marked as read-only, meaning they can only be written once; otherwise, an exception will be thrown.

class Post {
    public function __construct(
        public readonly string $title,
        ...
    ) {}
}
$post = new Post(title: ‘PHP 8.1 is available on Platform.sh’, /* … */);
// All good, life is great.

$post->title = 'Some other and less fancy title';
// Error: Cannot modify readonly property Post::$title

First-class callable syntax

PHP 8.1 introduces a new syntax in creating a callable. You can now make a closure from a callable by calling that callable and passing it ... as its argument:

function love(Human $a, Human $b) {
    /* What is love … */
}

$love = love(...);

$love(a: $someone, b: $someoneElse);

Using new in initializers

Now, the new keyword can be used in function definitions as a default parameter and can attribute arguments. This feature may considerably reduce the size of the classes, just as the constructor property promotion feature in PHP 8.0 did. Less noise, more signal, and more headspace for zen developers.

class FooController {
    public function __construct(
        private Logger $logger = new BarLogger(),
    ) {}
}

Fibers

Fibers are a new, low-level mechanism to ease concurrency. Enabling the creation of full-stack, interruptible functions that can be used to implement cooperative multitasking in PHP, Fibers can also be described as green threads as they execute code concurrently without relying on a multithreaded environment.

$catFiber = new Fiber(function (): void {
    // …
    $value = Fiber::suspend('taking a nap');
    echo ‘Cat fiber resuming its activity because of: ‘ . $value;
});
 
$value = $catFiber->start();
echo ‘Current value from this cat fiber: ‘ . $value;
// Current value from cat fiber suspending: taking a nap
 
$catFiber->resume(‘some random noise’);
// Cat fiber resuming its activity because of: some random noise

Although the Fibers feature doesn’t have multithreaded functionality, nor is it genuinely asynchronous, it still introduces a shift in PHP historical paradigms. Learn more about Fibers with the RFC.

Try PHP 8.1 on Platform.sh

PHP 8.1 introduces changes that may affect older code, so we recommend testing your application before updating to the new PHP version. And testing PHP 8.1 on Platform.sh couldn’t be easier.

Step 1. Create a new branch in your repository.

Step 2. Update your .platform.app.yaml file in that branch, and change the type key to the following:

type: php:8.1

Step 3. Git commit and deploy. That branch is now running on PHP 8.1.

Step 4. Give your application a try, run your integration tests, and check for updates.

Step 5. Once changes are made, merge the branch back to your main branch.

Congrats! You’re now running PHP 8.1 on production!

If you hit a roadblock with PHP 8.1, our Customer Support team is just a click away.