Just days after its official release, we are thrilled to announce the immediate availability of PHP 8.1 for all Grid plan 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.
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.
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;
}
Copy the snippet
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);
Copy the snippet
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',
};
}
}
Copy the snippet
interface HasColor
{
public function color(): string;
}
Copy the snippet
enum Status implements HasColor
{
// ...
public function color(): string { /* … */ }
}
Copy the snippet
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,
...
) {}
}
Copy the snippet
$post = new Post(title: ‘PHP 8.1 is available’, /* … */);
// All good, life is great.
$post->title = 'Some other and less fancy title';
// Error: Cannot modify readonly property Post::$title
Copy the snippet
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);
Copy the snippet
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(),
) {}
}
Copy the snippet
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
Copy the snippet
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.
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 couldn’t be easier. First, create a new branch in your repository. Then update your ./.upsun/config.yaml
file in that branch and change the type key to:
type: php:8.1
Copy the snippet
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!