Sign in Registration
ruen

What's new in PHP 8 - improvements and features that make it worth upgrading to this version

Language PHP for during its existence, it has gone through the path of improvement, becoming from a simple language for building web pages to a powerful tool. PHP 8 brings even more improvements and features. A lot of optimization work has been done - PHP 8 performance has improved. The syntax has also been improved and the type system is more robust. More details below.

chto-novogo-v-php-8

What's new in PHP 8

A short list of innovations worth looking out for:

- generic types (union types), which allow replacing PHPDoc annotations for union types ;

- named arguments, which means that you can pass only the required arguments, and in any order, just by specifying the argument names ;

- defining properties in the constructor, which allows less code to both define and initialize properties ;

The

- nullsafe operator (? ->) helps to avoid checking for null. It is enough to use a sequence of calls with this operator. Returns null if one of the elements in the tested sequence returns null ;

- added attributes, which means it is possible to use structural metadata with standard PHP syntax instead of PHPDoc annotations ;

- added a match expression, similar to switch, but match is a strongly typed expression and can be returned. It is possible to use only one-line expressions ;

- type consistency error handling for built-in functions, which allows such functions to now throw errors on invalid argument types ;

- JIT (Just-In-Time compilation), which allows PHP to speed up by about 3 times. But such an increase may not always be obtained, it is necessary to take into account the type of web application. Without this innovation, PHP 8 is equal in speed to PHP 7.4 ;

- changes in the comparison of strings and numbers. If the comparison is with a string containing a number, the number comparison will be used, otherwise the conversion and string comparison will be performed ;

- other improvements in type system, error handling and data consistency .

Improvements in PHP 8 type system and error handling

Let's take a closer look at this. The following has been done:

- strict type checking for some operators has been added ;

- error suppression with the @ operator for fatal errors no longer occurs ;

- added new type mixed ;

- now possible inheritance with private methods ;

- static type can be returned ;

- added method checking for abstract traits ;

- magic methods now have correct signatures ;

- reworked engine warnings ;

- now displays a fatal error if method signatures are incompatible ;

- for OpenSSL, Curl, XMLWriter, Gd, Sockets and XML Extensions now use opaque objects instead of resources ;

- added types for Email thread functions .

PHP 8 syntax changes

PHP 8 syntax has also been improved, such as the following:

- now it is possible to use a comma at the end of parameter lists and in the use list of closures, as previously could be done for arrays ;

- for the catch block, you can omit the variable ;

- syntax of some variables changed ;

- all names in the namespace are now treated as a separate token ;

- throw is now an expression ;

- it is now possible to use :: class for objects.

New PHP 8 Classes, Functions and Interfaces

These innovations will help make working with PHP even more convenient and productive:

- added the Weak Map class (a collection that accepts objects as keys and serves to create caches of data obtained from an object that do not need to exist longer than the object itself) ;

- added Stringable interface (class that implements the __toString () method) ;

- new function str_contains (determines if a string contains a given substring) ;

- str_starts_with (checks if a string starts with a given substring) ;

- str_ends_with (checks if a string ends with a given substring) ;

- fdiv (function for dividing one number by another according to the IEEE 754 specification) ;

- get_debug_type (gets the type of the variable for debugging) ;

- get_resource_id (get an integer ID for the specified resource) ;

- object-oriented token_get_all function (splits the passed code into PHP tokens) ;

- innovations for the DOM, new APIs for crawling and processing .

The discussed features and improvements will allow you to develop in PHP much more productive and convenient. If the project is using an old version, perhaps it's worth the upgrade to PHP 8 to increase productivity, security and usability in project development.

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.