Everything to know about the New PHP 8 update

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 built 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. The following features are not exhaustive and many more things will be seen when PHP 8 comes out.


1. UNION TYPES 2.0

Union types can now take values that can be of a different type. These are a combination of two or more types which tells that any one of them 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 a 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 the Union type.
  • The nullable type unions are also allowed. It can be written using |null, or by using? notation. 

Example:

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 in 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’s previous versions when an 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 behavior when starting with a 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:

0 {main}
thrown in /path/to/your/test.php on line 4

7. Throw Expression

In PHP series throw statement is not allowed to be used where expressions are allowed. PHP8 designs throw statements 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 :

// $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 the objective of removing and destroying the map keys that no longer have to be used. The new update will fix areas like memory leaks and performance. With PHP 8 the code will show the following results:

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 own. 

8. Trailing Commas

PHP 8 now gives trailing commas in a parameter list with functions, methods, and closures. As shown below :

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 example:- 

$foo = new Foo();
var_dump($foo::class);

10. Attributes v2

The annotations are metadata used to define properties of objects, elements, files, etc. PHP 8 shows it as :

<<PhpAttribute>>

class ExampleAttribute
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
}

There are several more new functions in 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 

A previous version of PHP was 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 a new type for searching inside the strings 

str_contains ( string $haystack , string $needle ) : bool

The best part is it is very easy to use. You can simply write the following codes :

$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.

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. In addition, it also solves class names. 

As the previous versions were not good for type checking. This new improvement is quite beneficial. 

The above changes are obvious changes that have been passed. Many more changes and improvements are pending on which work is going on. In the coming months, those changes will be clarified. These improvements with PHP8 make it very reliable and efficient. 


Leave a Reply

Your email address will not be published. Required fields are marked *