• Overview
    Key features
    • Observability
    • Auto-scaling
    • Multi-framework
    • Security
    Frameworks
    • Django
    • Next.js
    • Drupal
    • WordPress
    • Symfony
    • Magento
    • See all frameworks
    Languages
    • PHP
    • Python
    • Node.js
    • Ruby
    • Java
    • Go
  • Industries
    • Consumer Goods
    • Media/Entertainment
    • Higher Education
    • Government
    • Ecommerce
  • Pricing
  • Overview
    Featured articles
    • Switching to Platform.sh can help IT/DevOps organizations drive 219% ROI
    • Organizations, the ultimate way to manage your users and projects
  • Support
  • Docs
  • Login
  • Request a demo
  • Free Trial
Blog
Cover image

PHP 8.1 lays new ground on Platform.sh

php
21 December, 2021
Thomas di Luccio
Thomas di Luccio
DevRel Engineer

Just days after its official release, we are thrilled to announce the immediate availability of PHP 8.1 for all Grid plans projects. The new PHP foundation releases a new main version every year at the very end of November, and It’s some kind of early Christmas for us developers and application makers.

Sparks and acceleration

PHP 8.1 comes with many new and long-expected features such as Enums, Readonly properties, First-class callable syntax, and new in initializers.

It also provides some nice performance improvements estimated at around 5 to 8%. Dmitry Stogov, the author of this impressive feature, describes this as a new transparent technology that eliminates the overhead of PHP class inheritance.

And finally, Fibers could constitute no less than a groundbreaking shift in the PHP ecosystem by adding a low-level mechanism to manage parallelism.

Those functionalities and many more are available now. And you can give them a try today on Platform.sh.

Enums

This is a feature I’ve expected for years, and I cannot wait to use it as it is 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);

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

Class properties can be marked as read-only. This means they can only be written once, and an Exception will be thrown otherwise.

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);

new

This feature allows wider use of the new keyword that can now be used in function definitions as a default parameter and attribute arguments.

Along with the Constructor Property Promotion that came with PHP 8.0, this may considerably reduce the size of the classes. Less noise and more signal: more headspace for zen developers.

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

Fibers

Fibers are a new and low-level mechanism to ease concurrency. They allow the creation of full-stack, interruptible functions that can be used to implement cooperative multitasking in PHP. These are also described as green threads since 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

If this is not genuinely asynchronous or multithreaded functionalities, Fibers may still lay new ground for the PHP ecosystem as they introduce a shift in PHP historical paradigms. Learn more about Fibers with the RFC.

Try it out today

PHP 8.1 does introduce some breaking changes that may affect older code. You may want to test your application before updating the PHP version. Great news: testing PHP 8.1 on Platform.sh couldn’t be easier. First, create a new branch in your repository. Then update your .platform.app.yaml file in that branch and change the type key to:

type: php:8.1

Git commit and deploy: that branch is now running on PHP 8.1. Give your application a try, run your integration tests, and check if anything needs to be updated. Once you’ve made whatever changes are needed, merge that branch back to your main branch. Congratulations, you’re now running PHP 8.1 on production.

Happy Deploying, PHP!

Get the latest Platform.sh news and resources
Subscribe

Related Content

A festive treat: PHP 8.3 is already available on Platform.sh

A festive treat: PHP 8.3 is already available on Platform.sh

Company
AboutSecurity and complianceTrust CenterCareersPressContact us
Thank you for subscribing!
  •  
Field required
Leader Winter 2023
System StatusPrivacyTerms of ServiceImpressumWCAG ComplianceAcceptable Use PolicyManage your cookie preferencesReport a security issue
© 2024 Platform.sh. All rights reserved.
Supported by Horizon 2020's SME Instrument - European Commission 🇪🇺