What’s New In PHP 8.0?

  Solace  Infotech    January 19, 2021    240


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: <<Attribute>>.

class Foo
public const FOO = 'foo';

public $x;

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

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

function f1() { }

$f2 = <<ExampleAttribute>> function () { };
$f3 = <<ExampleAttribute>> 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-

In older versions, equivalent inheritance checks on public, protected and personal methods. In another way, private methods follow identical signature method rules as protected and public methods. Not making sense; notwithstanding, private methods won’t be accessible by child classes. This RFC grabbed hold to stop inheritance checks to be performed on private methods. Also, private function didn’t add up, so doing so will now show a warning as- Private methods are not conclusive as others never override them. 

6. Weak Maps RFC-

WeakMap implementation added in PHP 8. It holds references to things, disallows objects from being garbage collected. Consider an example of ORMs; generally they implement caches holding references to entity classes to improve relations between entities. Those entity objects can’t be garbage collected as long as this caches references them, although the cache is just a reference. Caching layer collaborates with weak references rather than maps. PHP will garbage collect these objects when nothing works. ORM’s help to manage several hundred, if not thousands of entities within an invitation, weak maps offer a resource-friendly way of handling objects.

class Foo
private WeakMap $cache;
return $this->cache[$obj]
??= $this->computeSomethingExpensive($obj);
Allowing::class on objects rfc

A small but useful, new feature:::class on objects instead of using get_class() on them. It works equivalently as get_class().

$foo = new Foo();

7. Non-Capturing Catches RFC-

Whenever PHP8 catches an expression, you store it during a variable, whether you used that variable or not. With non-capturing, it helps omit the variable, so instead this:

//Something goes wrong
} catch (MySpecialException $exception) {
Log::error(“Something went wrong”);
You can now do that :

Try {
// Something goes wrong
} catch (MySpecialException) {
Log::error(“Something went wrong”);

Remember that, it is necessary to specify the type: you’re not allowed to possess an empty catch. If you like to catch all exceptions and errors, you’ll use throwable due to catching type. 

8. Namespaced Names Being One Token RFC-

PHP won’t interpret each part of a namespace as a sequence of tokens. This kind of RFC changed that behavior, means reserved names and used in namespaces.

#Saner numeric strings rfc

PHP’s type system uses lots of smart things while encountering things. So it makes RFC behavior more clear and consistent.

9. Reflection Method Signature Changes-

3 Method signatures of reflection classes are changes:

Create DateTime objects from the interface

It allows you to create a DateTime object from a DateTimeImmutable object using DateTime::createFromImmutable($immutableDateTime), but opposite is considered tricky.

Adding DateTime::createFromInterface() and DatetimeImmutable::createFromInterface() 

Now, there is a generalised thanks to convert DateTime and DateTimeImmutable objects to each other.

DateTime::createFromInterface(DateTimeInterface $other);

DateTimeImmutable::createFromInterface(DateTimeInterface $other);


get_debug_type() returns the kind of a variable seems like something gettype()  would go? get_debug_type() returns more useful output for arrays, strings, anonymous classes and objects.

For instance, calling gettype() on a category \Foo\Bar would return object. By using get_debug_type() will return category name. A difference between get_debug_type() and gettype() is out there within the RFC.

Wrap Up-

There are some of the new features included in PHP 8.0. There can be few others too. Good thing about PHP 8 is, it is not about single feature addition but a complete range of services that’ll change developer’s lives to develop iconic enterprise solutions.

 Article keywords:
php 8.0


 Share this article: 
Print Digg StumbleUpon del.icio.us Facebook Yahoo! Buzz Twitter Google Bookmarks LinkedIn MySpace Orkut PDF Scoopeo Viadeo Add to favorites

© Copyright - Articles XP