PHP 8.0.0. This version marks the latest major version of the PHP language and comes with many new enhancements and features, such as:
- Union Types
- Named Arguments
- Match Expressions
- Attributes
- Constructor Property Promotion
- Nullsafe Operator
- Weak Maps
- Just In Time Compilation
Named arguments
htmlspecialchars ($ string, double_encode: false);
Attributes
# [Route ("/ api / posts / {id}", methods: ["GET", "HEAD"])] class User {
Constructor property promotion
class Point { public function __construct ( public float $ x = 0.0, public float $ y = 0.0, public float $ z = 0.0, ) {} }
Union types
class Number { public function __construct ( private int | float $ number ) {} } new Number ('NaN'); // TypeError
Match expression
echo match (8.0) { '8.0' => "Oh no!", 8.0 => "This is what I expected", }; //> This is what I expected
Nullsafe operator
$ country = $ session? -> user? -> getAddress ()? -> country;
Saner string to number comparisons
0 == 'foobar' // false
Consistent type errors for internal functions
strlen ([]); // TypeError: strlen (): Argument # 1 ($ str) must be of type string, array given array_chunk ([], -1); // ValueError: array_chunk (): Argument # 2 ($ length) must be greater than 0
Other syntax tweaks and improvements
Allow a trailing comma in parameter lists RFC and closure use lists RFC
Non-capturing catches RFC
Variable Syntax Tweaks RFC
Treat namespaced names as single token RFC
Throw is now an expression RFC
Allow :: class on objects RFC
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