The idea of this note is to write down PHP 8+ what's news briefly, so they are visible all at once on a single page. Like a cheat-sheet.
Keep in mind that these are just the basic features, see each version's link for more details.
-
Named function arguments (i.e.
arg_name: value, instead of filling preceding default arguments) - Attributes (instead of DocBlock comments)
- Constructor property promotion (properties in constructor instead of normal declaration)
-
Union types (i.e.
int|string $varname) -
Match expression (to replace some of
switch-case) - Nullsafe operator (instead of nesteds ifs)
-
Saner string to number comparisons (i.e.
0 == 'somestrval' // not true anymore) - Consistent type errors for internal functions (internal functions throws typed exceptions now)
- Just-In-Time compilation (2x performance in some specific long-running applications)
- and more
- Enumerations (enums)
- Readonly Properties (for VO and DTO)
-
First-class Callable Syntax (to get a reference to any function, i.e.
$fn = strlen(...);) - New in initializers (for nested attributes)
-
Pure Intersection Types (
Iterator&Countable $valuebut notA&B|C) - Never return type (to be clear that function returns nothing)
- Final class constants (cannot override functions and inherit classes)
-
Explicit Octal numeral notation
// TODO: add some description below - Fibers
- Array unpacking support for string-keyed arrays
- and more
- Readonly classes
- Disjunctive Normal Form (DNF) Types
- Allow
null,false, andtrueas stand-alone types - New "Random" extension
- Constants in traits
- Deprecate dynamic properties
- and more
- Typed class constants
- Dynamic class constant fetch
- New
#[\Override]attribute - Deep-cloning of readonly properties
- New
json_validate()function - New
Randomizer::getBytesFromString()method - New
Randomizer::getFloat()andRandomizer::nextFloat()methods - Command line linter supports multiple files
- and more
Top comments (2)
Very informative cheatsheet. Thank you for sharing.
Just realized I forgot to add one VERY useful link, if you need to migrate between PHP versions, it also includes PHP 7.x:
php.net/manual/en/history.php