Junior — Middle
What features and capabilities were added in PHP 8, and which of them do you use in your projects?
sobes.tech AI
Answer from AI
PHP 8 introduced many new features and improvements, including:
- Union Types — the ability to specify multiple types for parameters and return values.
- Named Arguments — calling functions with parameter names, which improves readability.
- Attributes (Annotations) — built-in support for metadata through attributes.
- Constructor Property Promotion — concise syntax for declaring and initializing properties in constructors.
- Match Expression — a more powerful and safer alternative to switch with value return.
- Nullsafe Operator (
?->) — simplified access to methods and properties of objects that may be null. - JIT compilation — performance enhancement through Just-In-Time compilation.
- Strict typing for str_contains, str_starts_with, str_ends_with and other new functions.
In my projects, I often use Named Arguments to improve code readability and Constructor Property Promotion to reduce boilerplate code in classes. I also actively use the Match Expression for cleaner logic selection.
Example of using Match Expression:
$result = match($statusCode) {
200, 201 => 'Success',
400 => 'Bad Request',
404 => 'Not Found',
default => 'Unknown status',
};