finally{}: What’s in PHP Eight?
By Eli White
While much of the world shuts down, the PHP core developers have been hard at work preparing for the release of PHP 8.0 at the end of this year! The feature freeze is in just a few months (July 28th), so this is the exciting time when there is a push to get various features into this momentous release! Let’s take a look at a few of the bigger things currently planned for PHP 8.0
Get the Full Issue
This article was published in the May 2020 issue of php[architect] magazine. Download the Free Article PDF to see how it looks in the published magazine.
If you'd like more articles like this one, become a subscriber today!. You can get each monthly issue in digital and print options or buy individual issues.
$object::class
The ::class
construct has existed since PHP 5.5 and allows you to get the name of a class as a string. However, it could only be called directly on the class itself. It makes sense to use this syntax on objects as well. So that is being implemented! This functionally equivalent to calling get_class()
on the object, but without needing the function wrapper.
Static Return Type
Hopefully, you are familiar with the static
keyword—technically special class name—which allows for late static binding in PHP. I have been known to use it perhaps a bit too much. Essentially it references the class a method was called on, even when that method was inherited. Consider the code in Listing 1.
Listing 1
<?php
class Author
{
public function who() {
echo "Eli\n";
}
public function name() {
static::who();
}
}
class Editor extends Author
{
public function who() {
echo "Oscar\n";
}
}
$e = new Editor();
$e->name();
The code echoes Oscar
which is what you’d expect and want. Using self
would instead have output Eli
. The new static
return type now does the same thing, evaluating the name of the class on resolution. For example, if we added the following to the above Author class:
public function fluent(): static {
// Do stuff
return $this;
}
Then a call to the Editor
class’ fluent()
method acts as having a return type of Editor
. And a call to Author
’s is Author
.
Union Types
When declaring a variable, parameter, or return type in PHP, you have always had to declare a single type. This behavior was updated recently to allow for a special either/or union of a type or null value using the ?type
syntax. However, a strong power of PHP has been it’s type flexibility. Being able to make a function that perhaps returns NULL
, a false
, or a string
, is handy sometimes. The type system is being updated to allow for that. So anywhere you can currently place types, you’ll be able to union multiple possible types together with the |
syntax such as:
function weird(int|float $number): null|boolean|string {
/* Do things */
}
Along with this, a new pseudo-type of false
is being created. It allows a function that returns a value or false, as many older PHP functions do, to declare their intended usage. Similarly, while you can still use the ?
notation to denote a “type or null” situation, you can also now specify that directly as I did in the example above.
str_contains()
For decades, PHP developers have repurposed strpos()
and strstr()
to check if a string exists inside of another one. However, the way you use these is often confusing to newer developers (especially with the need to check false
versus 0
. PHP 8.0 fixes that by adding a new function that returns a boolean to indicate whether one string exists inside of another:
str_contains ( string $haystack , string $needle ) : bool
JIT (Just in Time Compiler)
PHP 7 brought some substantial performance gains, which were initially inspired by some work on creating a JIT for PHP. While gains were found during the process, the JIT itself wasn’t released because it was difficult to maintain and wasn’t showing any positive speed improvements for web applications at the time.
Well, it’s ready to be released now. It’s been updated and augmented. Meanwhile, the nature of many web apps has changed to now make it even more relevant. It’s implemented as a part of OPCache. However, you can toggle it on or off independently.
Currently, it doesn’t appear to make web applications, such as WordPress, any faster; however, in CPU intensive workloads within PHP, it is showing up to a 2x speed increase.
Summary
This overview has been just a personal highlight reel of things coming to PHP 8. There are lots of other enhancements and fixes coming as well, with three months for new features to still make it in! As we approach the end of 2020 and into 2021, it’s going to be an exciting time for PHP!
Leave a comment
Use the form below to leave a comment: