Top Features & Functions Introduced In New PHP 8

Shibani Sharma
Code Like A Girl
Published in
8 min readJul 20, 2020

--

PHP’s new version i.e. 8.0 is expected to be released on December 3, 2020. Nowadays people have an eye on php8 updates as this will come with major and vital modifications.

PHP 8.0 version will for sure help developers by simplifying the complex tasks and upgrading the new features. This post will help you in knowing the features of the php 8 version and as per the latest updates the post will be simultaneously updated. Many php Developer are working on this update and this latest release will for sure benefit the IT outsourcing company.

As per the W3Techs’ data, with known server-side programming language PHP is utilized by 78.9% of all websites which means almost 8 out of every 10 websites which you visit uses PHP.

PHP Market Position

Source: W3Techs’ data

PHP 8 is a new higher version that’s why there is a high chance of code-breaking and to avoid the same it is necessary to make vital modifications as per the new release.

The new release of the 8.0 version will bring some mind refreshing updates and features and it may include JIT compiler, union types, attributes, and so on.

New Features In PHP 8

Currently the PHP 8 version is not released and its development is under active state so more additions of features and other updates may also be added later in the provided list.

Union types

This is one of the new dynamically typed nature features of the PHP family. Union types are a set of two or more types which symbolize that either one of those can be used.

Always remember that Nullable unions can be addressed using |null or existing ? notation: but void cannot be the part of union features as it symbolizes “no return value at all”.

JIT

JIT is already available in the PHP but in the 8.0 version its performance and usability will be increased. For doing experiments and analyzing the other factors in PHP 7.4 version JIT feature was available in the disabled by default mode.

In relation to V8, HHVM, PyPy, and other modern PHP JIT is very simple, but it boosts the level of the entire PHP complexity, risk of new sort of bugs, and price of development and maintenance.

Attributes

Attributes are a form of syntactic metadata to declarations of classes, structured, properties, functions methods, constants, and parameters. This feature mainly permits you to determine configuration directives directly submerged with a declaration of that code.

Attributes feature is also available in other programming languages namely C#, C++, Rust, Hack and by the name Annotation the same feature is available in Java.

New static return type

The main purpose of new static is that it can be used as a return type next to already usable “self” and “parent” types.

The static class is only authorized inside return types, where it may also appear as part of a complex type expression like; ?static or static|array.

New Mixed Type

This is the new feature that will be introduced in PHP 8 and this will surely simplify the developer’s task as previously developers need to specify all functions but this time by just mentioning mixed you will be able to define a few function lists.

Mixed type includes array | bool | callable |int | float | null | object | resource | string so now you can use all these functions by just typing one word. Mixed can be used as a parameter or property type but not as a return type.

Throw Expression

This feature will allow you to throw exceptions where expressions are allowed like arrow functions, the coalesce operator, etc whereas currently, it is impracticable to throw exceptions in trades where only expressions are allowed.

Weak Maps

Weak References were added in PHP 7.4 and upon this weak maps will be built. WeakMaps hold sources to objects, which doesn’t restrict those objects from being garbage collected.

If an object is employed as a weak map code garbage collected then it removes that code from the weak map.

Allowing :: Class Objects

This new feature will just act like a small packet big blast as this will allow developers to use ::class on objects, rather than using get_class() on them.

$object::class is already provided on the syntax level but currently it shows a compile error. This error will be dismissed when utilized in a normal expression context. But, $object::class will remain blocked inside a constant expression context as objects cannot be created there.

Trailing comma in parameter lists

This feature is already available as this can be used while applying function but after releasing the php8 version you will be able to use trailing comma support in parameter lists as currently, it does not support parameter lists for functions, methods, and closures.

Create DateTime objects from interface

You can already create a DateTime object from a DateTimeImmutable object using By using DateTime::createFromImmutable($immutableDateTime) you can create DateTime object from a DateTimeImmutable, but this feature introduced generalised way to convert DateTime and DateTimeImmutable objects.

New Stringable Interface

This New Stringable interface offers a new Stringable interface that is automatically appended to classes that perform the __toString() method. It mainly has 2 goals i.e.

It permits string|Stringable to express string|object-with-__toString()

It has provided a forward upgrade path from PHP 7 to 8.

New fdiv() function

This new fdiv() function is somehow alike to fmod() and intdiv() functions that allow division by 0. On applying this function you will get INF, -INF, or NAN (depend on the applied case) instead of an error.

New get_debug_type() function

This feature’s main purpose is to add a new function get_debug_type that will return the given type of a variable. This function will help you to generate debugging messages when dealing with types that cannot be managed by the present PHP runtime.

get_debug_type() provides more useful output for strings, arrays, anonymous classes, and objects.

Object implementation of token_get_all()

The token_get_all() function delivers an array value. This object adds a PhpToken class with PhpToken::getAll() order. This implementation runs with objects instead of plain values. It utilizes shorter memory and is simple to read.

Variable syntax tweaks

The Uniform Variable Syntax RFC fixes a number of instabilities in PHP’s variable syntax. This RFC aims to direct a small handful of cases that were overlooked. This RFC addresses the small overlooked cases.

New str_contains() functions

To know whether a string contains another string you don’t have to rely on strpos as by this str_contains() function you can get what you exactly want.

Just Use this:

Instead of this:

New str_starts_with() and str_ends_with() functions

These two other functions are newly added to the php8 core. It can be used as:

Abstract Methods In Traits Improvements

By writing the below mentioned code you will be able to get improved traits in an abstract method.

Use This:

Instead of this:

Reflection method signature changes

3 changes have been introduced in Reflection method signature :

ReflectionClass::newInstance($args);

ReflectionFunction::invoke($args);

ReflectionMethod::invoke($object, $args);

Now it become:

ReflectionClass::newInstance(…$args);

ReflectionFunction::invoke(…$args);

ReflectionMethod::invoke($object, …$args);

You can also support PHP 7 and PHP 8 b y using the below mentioned signature:

ReflectionClass::newInstance($arg = null, …$args);

ReflectionFunction::invoke($arg = null, …$args);

ReflectionMethod::invoke($object, $arg = null, …$args);

PHP 8 Breaking Changes

Consistent Type Errors

When user-defines the functions then it results in a TypeError, but internal functions show a warning and return null. This feature aims to consistently produce TypeError exclusions for all invalid parameter types, despite whether the function is extension-defined or user-defined.

Reclassified engine warnings

Most of the triggered warnings or notices will be now converted into proper errors. Let’s read some changed warnings -

  • Undefined variable converted into Error.
  • Undefined array index converted into warning.
  • Division by zero is converted into Error.
  • Non-object Increment/decrement, modify, and assign property eg; ‘%s’ will show Error.
  • On generating default objects from empty value will display Error.
  • Undefined property such as %s::$%s: will show a warning.
  • If in an already occupied array you will add the next element then it will show Error, and so on…

Default error reporting level

This time in PHP 8 all the silently ignored errors will pop up and for resolving this developers have to take corrective measures.

Concatenation precedence

This is available in PHP 7.4 but this time in php8 it is applied by taking corrective measures means, if we write echo “sum: “ . $a + $; then it will interpret like this echo (“sum: “ . $a) + $b; but after releasing the new version it will display like echo “sum: “ . ($a + $b);

Stricter type checks for arithmetic and bitwise operators

After releasing the PHP 8 it was possible to apply arithmetic or bitwise operators on arrays, resources, or objects as on applying the same it will show TypeError.

Wrapping Up

In this blog all the latest updates related to php 8 are mentioned. Some updates are older and are previously introduced in the old PHP versions but this time PHP will enhance the performance of a few older updates. Apart from the updates in PHP 8.0 many new features and functions are projected to be added.

If you want to read about the latest PHP release then you can read :

--

--

Hi i am an independent Technical Content Strategist working with IT Companies. Gives a voice to Programming languages & Software development. Find me on Quora