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

 

  1. UNION TYPES 2.0
  2. Just in Time Compiler
  3. Incompatible Method Signatures
  4. Arrays Starting With a Negative Index
  5. Frequent Type Error for Internal Functions
  6. Throw Expression
  7. Weak Maps
  8. Trailing Commas
  9. Allowing ::class on objects
  10. 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:

 

WPOven – The Dedicated WordPress Hosting provides you with –

 

  1. FREE SSD Storage
  2. FREE SSL CERTIFICATE
  3. FREE Business Email
  4. FREE Over 100 premium themes and plugins
  5. FREE Daily Off-Site Backups
  6. FREE Malware Screening & Cleanup 
  7. FREE WordPress Dev & Server Support
  8. Dedicated Server

 

Our plans start at $16.61.  With dedicated VPS, dedicated resources, server stack, etc

 

wpoven hosting

 

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:

 

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 :

 

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 :

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 :

 

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 

 

10. Attributes v2

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

 

<<PhpAttribute>>

 

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 

 

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

 

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.

 

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. 

Leave a Reply

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