PHP 8.0 Released Major Updates and more features
29 Jun, 2022PHP 8.0 is a major update of the PHP language.
It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency.
Named arguments
- Specify only required parameters, skipping optional ones.
- Arguments are order-independent and self-documented.
Attributes
Instead of PHPDoc annotations, you can now use structured metadata with PHP's native syntax.
Constructor property promotion
Less boilerplate code to define and initialize properties.
Union types
Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.
Match expression
The new match is similar to switch and has the following features:
- Match is an expression, meaning its result can be stored in a variable or returned.
- Match branches only support single-line expressions and do not need a break; statement.
- Match does strict comparisons.
Nullsafe operator
Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator. When the evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain evaluates to null.
Saner string to number comparisons
When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison.
Consistent type errors for internal functions
Most of the internal functions now throw an Error exception if the validation of the parameters fails.
Type system and error handling improvements
- Stricter type checks for arithmetic/bitwise operators
- Abstract trait method validation
- Correct signatures of magic methods
- Reclassified engine warnings
- Fatal error for incompatible method signatures
- The @ operator no longer silences fatal errors.
- Inheritance with private methods
- Mixed type
- Static return type
- Types for internal functions Email thread
- Opaque objects instead of resources for Curl, Gd, Sockets, OpenSSL, XMLWriter, and XML extensions
Other syntax tweaks and improvements
- Allow a trailing comma in parameter lists and closure use lists
- Non-capturing catches
- Variable Syntax Tweaks
- Treat namespaced names as single token
- Throw is now an expression
- Allow ::class on objects
New Classes, Interfaces, and Functions
- Weak Map class
- Stringable interface
- str_contains(), str_starts_with(), str_ends_with()
- fdiv()
- get_debug_type()
- get_resource_id()
- token_get_all() object implementation
- New DOM Traversal and Manipulation APIs
For source downloads of PHP 8 please visit the downloads page. Windows binaries can be found on the PHP for Windows site. The list of changes is recorded in the ChangeLog.
The migration guide is available in the PHP Manual. Please consult it for a detailed list of new features and backward-incompatible changes.
post source from : https://www.php.net/