DEV Community

Cover image for What’s New In PHP 8.0?
Solace Infotech Pvt. Ltd.
Solace Infotech Pvt. Ltd.

Posted on

What’s New In PHP 8.0?

PHP is continuously evolving and PHP 8.0 is released on November 26th, 2020. It is a mega edition as it explores a lot of features and performance improvements and deprecations to the language. The most talked about feature is the JIT compiler. Performance improving features like JIT deserve the popularity, the syntactical improvements may have more of a true impact for PHP practitioners- in the short term.

Here, we’ll discuss some of the notable features and improvements in PHP 8, including the JIT compiler and syntactical improvements that developers will surely like.

What’s New Features And Improvements In PHP 8.0?

1. JIT (Just In Time) Compiler-
One of the most exciting additions to PHP 8 is JIT compiler. As we all know that, php is an interpreted language, means it runs in real time, instead of being compiled and run at launch. JIT brings compiled code to PHP, and with it, better performance in some situations. It you’re working with web applications as most PHP developers are, JIT will not help much as these performance benchmarks show. However with the tasks like 3D rendering, data analysis, artificial intelligence and other long-running processes, it makes a huge difference. These are not common applications of PHP, but many developers are branching out, so this makes the engine more flexible. JIT makes certain to open PHP’s horizons and bring in devs interested in trying new things. For already existing projects, it might not do more. Prior to implementing JIT, ensure that you’ve tested it in an isolated environment and see whether it improves your performance or not.

2. Attributes-
Now, PHP supports attributes, or small pieces of metadata you add to parts of your code: functions, classes, parameter etc. Which means you don’t need to use docsblocks as a workaround.

You can add various attributes to any part of the code, import them with use statements and add parameters to attributes also. Add attribute to your code with signs: <>.
<>
class Foo
{
<>
public const FOO = 'foo';

<>
public $x;

<>
public function foo(<> $bar) { }
}

$object = new <> class () { };

<>
function f1() { }

$f2 = <> function () { };
$f3 = <> fn () => 1;

Note that attributes are not backwards compatible and will cause errors if ported into older versions of PHP. The docblock workaround is functional, however it’s always been somewhat clunky. Now, you can add attributes directly. For such a small addition, it has huge implications.

3. Union Types-
Key parts of PHP are, assigning a variable as an integer, boolean, null and so on. But prior you could only assign a variable with a single type. Now, they can be assigned with two or more types: a union type! For instance, you can assign integer and float type, and it can use either one of those. These are specified with line between each type, for example: int|float. You can not combine void and also duplicate or redundant types like int|int are also not allowed.

You could already use PHPDoc annotations to make something like a functional association type, but now you can avoid the inconvenient workarounds and simply assign variables with different types.

4. Match Expressions-
It eliminates the guesswork associated with determining whether failure to break within the switch case is intended or not, and simplifies the common pattern of assigning a value according to match. When used, the value you pass to match() is compared with the expression is on the left hand side. Whether it is a value or an expression, the value you pass to match() should match it for it to be selected. When matched, the expression on the right is evaluated and its return value returned; expressions must be callables or a lambda functions, and no multi-line closures are allowed.

5. Inheritance With Private Method-
Know more at- [https://solaceinfotech.com/blog/whats-new-in-php-8-0/]

Top comments (0)