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.
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.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes