PHP 8 is scheduled to be launched in December 2020. The new version of PHP comes with many expectations from the user. It is supposed to be bucked with many varieties of features and improved performance. The changes will make PHP faster and more efficient. Because of the drastic changes in the features, users might need to make some changes in their codes to get PHP 8 functional.
Its new features include :
- the JIT compiler
- union types
- attributes etc.
There will be many more features that will be added to the list.
Meaning of PHP
PHP is a Hypertext Preprocessor that is used for web development. It is very easy to use. And it is still used by highly professional programmers to develop. What makes it the best is that it supports major protocols such as IMAP, POP3, and LDAP. Many previous version of PHP was categorically good for the purpose. PHP 8 comes with many interesting features. Some of them are explained below.
New Features in PHP 8
The users can expect many new features from the new update. Changes in PHP 8 are made to make it more effective and reliable. Following features are not exhaustive and many more things will be seen when PHP 8 comes out.
- UNION TYPES 2.0
- Just in Time Compiler
- Incompatible Method Signatures
- Arrays Starting With a Negative Index
- Frequent Type Error for Internal Functions
- Throw Expression
- Weak Maps
- Trailing Commas
- Allowing ::class on objects
- Attributes v2
1. UNION TYPES 2.0
Union types can now take values that can be of a different type. These are basically a combination of two or more types which tells that any one of it can be used. Before coming of PHP 8 the PHP union types could only accept in phpdoc annotations.
With the new version, you do not have to rely on online documentation. The support will define union types with a T1|T2|.
Union types will be adapting all types except few (some limitations) :
The type VOID cannot be supported by Union because it means that the function does not return any value.
- The null type can be backed by Union type.
- The nullable type union are also allowed. It can be written using |null, , or by using? notation.
E.g:
1 2 3 |
public function foo(Foo|null $foo): void; public function bar(?Bar $bar): void; |
WPOven – The Dedicated WordPress Hosting provides you with –
- FREE SSD Storage
- FREE SSL CERTIFICATE
- FREE Business Email
- FREE Over 100 premium themes and plugins
- FREE Daily Off-Site Backups
- FREE Malware Screening & Cleanup
- FREE WordPress Dev & Server Support
- Dedicated Server
Our plans start at $66.63. With dedicated VPS, dedicated resources, server stack, etc
2. Just in Time Compiler
There will be landmark changes with JIIT. It translates the intermediate code into machine code. It will be able to bring major changes with data usage and performance. Changes are yet to be disclosed.
Read our old guide to get a better understanding of How PHP 7 Vs HHVM compares??
3. Incompatible Method Signatures
With PHP 8, the code will prompt with the following message unlike previous versions:
Fatal error: Declaration of C2::method(int $a) must be compatible with C1::method(array $a) in /path/to/your/test.php on line 7
4. Arrays Starting With a Negative Index
In PHP previous versions when array starts with a negative index, many indices would start with 0. But this is not the case with the new version. The array tends to change its behaviour when starting with negative Index.
5. Frequent Type Error for Internal Functions
Internal functions respond in a variety of ways depending on the situation. There has been constant inconsistency in the responses. PHP 8 edits these responses. It will show the message as :
Fatal error: Uncaught TypeError: strlen(): Argument #1 ($str) must be of type string, object given in /path/to/your/test.php:4
Stack trace:
1 2 3 |
#0 {main} thrown in /path/to/your/test.php on line 4 |
6. Throw Expression
In PHP series throw statement is not allowed to be used where expressions are allowed. PHP8 designs throw statement to be used wherever required. Like in arrow functions, null coalesce, operator, ternary and Elvis operators, etc.
This can better be explained with the following examples :
1 2 3 |
// $value is non-nullable. $value = $nullableValue ?? throw new InvalidArgumentException(); |
7. Weak Maps
It is a pile of data in which keys are weakly referenced. This was made with an objective of removing and destroying the map keys that no longer have to be used. New update will fix areas like memory leak and performance. With PHP 8 the code will show the following results :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
object(WeakMap)#1 (1) { [0]=> array(2) { ["key"]=> object(stdClass)#2 (0) { } ["value"]=> int(42) }< } |
When you want to remove the code, the key will be removed on its on.
8. Trailing Commas
PHP 8 now gives trailing commas in parameter list with functions, methods, and closures. As shown below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Foo { public function __construct( string $x, int $y, float $z, // trailing comma ) { // do something } } |
9. Allowing ::class on objects
Now, this feature allows you to use ::class on objects, rather than having to use get_class() on them.
For eg
1 2 3 |
$foo = new Foo(); var_dump($foo::class); |
10. Attributes v2
The annotations are metadata used to define properties of objects, elements, or files etc. PHP 8 shows it as :
<<PhpAttribute>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class ExampleAttribute { public $value; public function __construct($value) { $this->value = $value; } } |
There are several more new functions to the language :
It gives a variety of new functions to the language
- str_contains
- str_starts_with() and str_ends_with()
- get_debug_type
str_contains
Previous version of PHP used to provide developers with strstr and strpos. The issue with these two types is that both options are considered very intriguing. It cannot be used easily. PHP 8 gives new type for searching inside the strings
1 |
str_contains ( string $haystack , string $needle ) : bool |
Best part is it is very easy to use. You can simply write the following codes :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$mystring = 'Managed WordPress Hosting'; $findme = 'WordPress'; if (str_contains($mystring, $findme)) { echo "The string has been found"; } else { echo "String not found"; } |
str_starts_with() and str_ends_with()
Two more functions give the opportunity to search inside the string. The options check whether the given string ends or starts with another string.
1 2 |
str_starts_with (string $haystack , string $needle) : bool str_ends_with (string $haystack , string $needle) : bool |
They can also reply false if the needle is no longer than $haystack.
get_debug_type
This function returns the type of variable. It works the same as gettype function. But in addition, it also solves class names.
As the previous versions were not good for type checking. This new improvement is quite beneficial.
Above changes are obvious changes that have been passed. Many more changes and improvement are pending on which work is going on. With the next coming months, those changes will be clarified. These improvements with PHP8 makes it very reliable and efficient.