New features coming to PHP 8 in 2020!

PHP 8 will be an exciting release with lots of new additions to the language, coming out on December 3 2020. Becoming the next major release and moving forward away from PHP 7 which was released back in 2015 and we saw some major developments on the v7 branch!

With the next major PHP version not long away, let’s go over some features:

  1. Stringable interface: automatically adds the interface Stringable to classes that implement __toString() so you can typehint string|Stringable to allow everything that either is a string or can be converted to a string. RFC here.

  2. Allow ::class on objects: Adds support for $object::class. A small change but alows you to use ::class on objects instead of the get_class() function. RFC here.

  3. Static return type: Make static also usable as a return type (next to the already usable self and parent types). RFC here.

  4. Variable Syntax Tweaks: Fixes a number of inconsistencies in PHP’s variable syntax, Interpolated and non-interpolated strings, Constants and magic constants, Constant dereferencability, Arbitrary expression support for new and instanceof. RFC here.

  5. Union Types v2: The ability to type-hint a list of types, one of which needs to be fulfilled. E.g. a function doesStuffWithNumbers(int|float $num):int|float {...} takes an int or float and returns an int or float. RFC here.

  6. DOM Living Standard API: Upgrades DOM API to latest standard version. Working with XML documents is a necessary task for many web applications and the DOM extension implements a standardized API. The standard has evolved and is now a Living Standard similar to HTML 5 and continuously evolving and maintained by the Web Hypertext Application Technology Working Group (WHATWG). RFC here.

  7. Fatal error for incompatible method signatures: Inheritance errors due to incompatible method signatures currently either throw a fatal error or a warning depending on the cause of the error and the inheritance hierarchy. In PHP 8 it will throw a fatal error instead. RFC here.

  8. Arrays starting with a negative index: Any array that has a number n as it’s first numeric key will have for it’s next implicit key either n+1 if n >= 0 or 0 if n < 0 and in PHP 8 it will be changed to make this consistent by always using n+1 regardless of the sign of n. RFC here.

  9. Consistent type errors for internal functions: User-defined functions, passing a parameter of illegal type results in a TypeError . For internal functions, the behaviour depends on multiple factors, but the default is to throw a warning and return null. In PHP 8 the plan is to change this to consistently generate TypeError exceptions for all invalid parameter types, regardless of whether the function is user-defined or extension-defined. RFC here.

  10. Just in Time (PHP JIT): The reason behind some of the performance improvements for PHP 7 as the JIT project started the progress to improve PHP in other ways but, it is finally becoming an addition to PHP in version 8. Just like how JIT works in JVM, PHP will have its own, allowing the language to be used for more complex CPU intensive workloads, spreading the usage of where PHP can go. It has been said that the performance of PHP cannot be further improved if a JIT is not in place first. RFC here.

  11. Weak Maps: As long as you have a reference to something, it can’t be garbage collected, i.e. the memory can’t be freed (because you could still use it with that reference). But sometimes you want to keep track of a bunch of objects but don’t prevent them from being collected. That’s what weak maps can do. RFC here.

  12. Additional String Functions: With PHP 8 an array of extra str_* functions will be added. We get to see str_contains which checks if a string is contained in another string and returns a boolean value (true / false) whether or not the string was found. We will also see str_starts_with() and str_ends_with(). RFC for str_contains and the other string functions.

  13. Debug Type Function: adds a new function get_debug_type() that will return the given type of a variable, easier setup for debugging messages when dealing with types which cannot be handled by existing PHP runtime checking based on parameter types, such as those coming from an array. RFC here.

  14. New PHP object for token_get_all(): adds a PhpToken class with a PhpToken::getAll() method. Works with objects instead of plain values. It consumes less memory and is easier to read, returning an array of objects instead, even single-char tokens. RFC here.

  15. Native Support for Attributes: Adds proper support for creating attributes in code rather than using docblocks. Allows you to add meta data to classes, functions, properties, parameters and constants. It uses the syntax of <<ExampleAttribute>>. RFC here.

So there we have 15 RFCs that will make it into PHP 8! There are a few more things which are going to be added but these are the main ones for now.

Please also take a read of What's new in PHP 8 - stitcher.io, great post on the topic as well!

Let me know what you think, what are you most looking forward to using? :slight_smile: I think for me, I am looking forward to what the JIT compiler can do for PHP!

3 Likes

The JIT compiler looks to be the most interesting part! I’m looking forward to see how much it boosts performance

2 Likes