Ikodes Technology

What’s new in PHP 8?
PHP 8 was delivered on November 26, 2020. You can download it here. It's another significant rendition, which implies that there are some breaking changes, just as bunches of new highlights and execution enhancements.

Due to the breaking changes, there's a higher possibility you'll have to roll out certain improvements in your code to make it run on PHP 8. In the event that you've stayed up with the latest with the most recent deliveries however, the redesign shouldn't be excessively hard, since most breaking changes were belittled before in the 7.* adaptations. Furthermore relax, this large number of censures are recorded in this post.

Other than breaking changes, PHP 8 likewise brings a pleasant arrangement of new highlights like the JIT compiler, association types, traits, and the sky is the limit from there.

Union types rfc

Given the dynamically typed nature of PHP, there are lots of cases where union types can be useful. Union types are a collection of two or more types which indicate that either one of those can be used.

public function foo(Foo|Bar $input): int|float;
Note that void can never be part of a union type, since it indicates "no return value at all". Furthermore, nullable unions can be written using |null, or by using the existing ? notation:

public function foo(Foo|null $foo): void;

public function bar(?Bar $bar): void;

#JIT rfc
The JIT — just in time — compiler promises significant performance improvements, albeit not always within the context of web requests. I've done my own benchmarks on real-life web applications, and it seems like the JIT doesn't make that much of a difference, if any, on those kinds of PHP projects.

If you want to know more about what the JIT can do for PHP, you can read another post I wrote about it here.

#The nullsafe operator rfc
If you're familiar with the null coalescing operator you're already familiar with its shortcomings: it doesn't work on method calls. Instead you need intermediate checks, or rely on optional helpers provided by some frameworks:

$startDate = $booking->getStartDate();

$dateAsString = $startDate ? $startDate->asDateTimeString() : null;
With the addition of the nullsafe operator, we can now have null coalescing-like behaviour on methods!

$dateAsString = $booking->getStartDate()?->asDateTimeString();
You can read all about the nullsafe operator here.

#Named arguments rfc
Named arguments allow you to pass in values to a function, by specifying the value name, so that you don't have to take their order into consideration, and you can also skip optional parameters!

function foo(string $a, string $b, ?string $c = null, ?string $d = null)
{ /* … */ }

foo(
b: 'value b',
a: 'value a',
d: 'value d',
);
You can read about them in-depth in this post.

#Attributes rfc
Attributes, commonly known as annotations in other languages, offers a way to add meta data to classes, without having to parse docblocks.

As for a quick look, here's an example of what attributes look like, from the RFC:

use App\Attributes\ExampleAttribute;

#[ExampleAttribute]
class Foo
{
#[ExampleAttribute]
public const FOO = 'foo';

#[ExampleAttribute]
public $x;

#[ExampleAttribute]
public function foo(#[ExampleAttribute] $bar) { }
}
#[Attribute]
class ExampleAttribute
{
public $value;

public function __construct($value)
{
$this->value = $value;
}
}
Note that this base Attribute used to be called PhpAttribute in the original RFC, but was changed with another RFC afterwards. If you want to take a deep dive into how attributes work, and how you can build your own; you can read about attributes in-depth on this blog.

 

 



Author Bio

ikodes Technology

Manager of Marketing for Red Stag Fulfillment, an ecommerce fulfillment warehouse that was born out of ecommerce. He has years of experience in ecommerce and business development. In his free time, Jake enjoys reading about business and sharing his own experience with others.

Go Back

Expand Your Digital Horizons With Us

Start a new project or take an existing one to the next level. Get in touch to start
small, scale-up, and go Agile.

    Math Captcha + 79 = 87

    Leave a Reply