- JIT Compiler: The Just-In-Time (JIT) compiler improves PHP’s performance by compiling parts of the code at runtime.
- Union Types: Allows specifying multiple types for a single variable or function parameter.
Example:
function foo(string|int $input): void { /* ... */ }
- Match Expressions: A more concise and powerful alternative to switch statements.
Example:
$result = match ($input) {
1 => 'one',
2 => 'two',
default => 'unknown',
};
- Nullsafe Operator: Simplifies null-checking when accessing object properties or methods.
Example:
$result = $user?->getAddress()?->getCountry();
- Constructor Property Promotion: Simplifies class property declaration and constructor assignment.
Example:
class User {
public function __construct(
public string $name,
public int $age,
) {}
}
- Named Arguments: Allows passing arguments to functions by name, improving readability.
Example:
function foo(string $a, int $b, bool $c): void { /* ... */ }
foo(a: 'test', b: 42, c: true);
- Attributes: Metadata that can be added to classes, methods, properties, and more.
Example:
#[ExampleAttribute]
class Foo { /* ... */ }
- Weak Maps: A memory-efficient data structure for storing object references without preventing garbage collection.
Example:
$weakMap = new WeakMap();
$object = new stdClass();
$weakMap[$object] = 'data';
- FFI (Foreign Function Interface): Allows calling C functions and using C data types directly from PHP.
Example:
$ffi = FFI::cdef("
int printf(const char *format, ...);
", "libc.so.6"); // Replace "libc.so.6" with the actual library path
$ffi->printf("Hello, %s!\n", "World");
- Preloading: Improves performance by preloading frequently used PHP files into memory.
Example:
// In php.ini
opcache.preload=/path/to/preload.php - Typed Properties: Allows declaring types for class properties.
Example:
class User {
public string $name;
public int $age;
}
- Spread Operator in Array Expression: Allows unpacking arrays and merging them into a new array.
Example:
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$merged = [...$array1, ...$array2]; // [1, 2, 3, 4, 5, 6]
- Arrow Functions: Shorter syntax for anonymous functions with automatic variable capturing.
Example:
$squared = array_map(fn($n) => $n ** 2, [1, 2, 3, 4]);
- Improved Error Handling: More informative error messages and better handling of fatal errors.
Example:
// PHP 7.x: “Fatal error: Uncaught Error: Call to undefined function foo()”
// PHP 8.x: “Fatal error: Uncaught Error: Call to undefined function foo() in /path/to/file.php:42” - Fibers: Provides lightweight concurrency for PHP, allowing non-blocking code execution.
Example:
$fiber = new Fiber(function () {
echo "Fiber started\n";
Fiber::suspend();
echo "Fiber resumed\n";
});
$fiber->start();
echo "Main script\n";
$fiber->resume();
- Custom Object Serialization: Allows customizing object serialization using the
__serialize()
and__unserialize()
magic methods.
Example:
class MyClass {
public function __serialize(): array { /* ... */ }
public function __unserialize(array $data): void {
/* ... */
}
}
- Improved Type System: Better type checking and support for mixed, static, and never types.
Example:
function foo(): mixed { /* ... */ }
function bar(): static { /* ... */ }
function baz(): never { throw new Exception(); }
- Deprecations and Removals: Removal of deprecated features and functions, such as the
create_function()
function and thereal
type.
These features aim to improve your PHP development by providing better performance, enhanced type safety, and more expressive syntax.