PHP 8 Features Explained: From 8.0 to 8.5 – Complete Guide to New Enhancements and Changes

Yogesh Kushwaha
By Yogesh Kushwaha

February 21, 2024 11:12 AM

PHP 8 Features

PHP 8 introduced a series of disrupting features that transformed the language into a modern, performant, and developer-friendly platform. In this article, we’ll summarize all major changes from PHP 8.0 to PHP 8.5, with examples and practical insights.

PHP 8.0 - The big move

It was a major upgrade in PHP from PHP 7.x versions. Here are key features of PHP 8.0

  • JIT Compiler – Improves performance for CPU-heavy tasks.

PHP JIT is implemented as an almost independent part of OPcache. It may be enabled/disabled at PHP compile time and at run-time. When enabled, native code of PHP files is stored in an additional region of the OPcache shared memory and op_array->opcodes[].handler(s) keep pointers to the entry points of JIT-ed code. This approach doesn't require engine modification at all.

  • Union Types – Combine multiple types in function signatures.

Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.

class Number {
    public function __construct(
        private int|float $number
    ) {}
}

new Number('NaN'); // TypeError
  • Named Arguments – Pass arguments by name.
function devide(int $divident, int $devider): float {
    return $divident / $devider;
}

$result = devide(devider: 2, divident: 10);
echo "Result of division: " . $result . "\n"; // Output: 5

// Even if you change the order of arguments, outcome doesn't cahnge.
$result = devide(divident: 10, devider: 2);
echo "Result of division: " . $result . "\n"; // Output: 5
  • Match Expression – Cleaner alternative to switch.
$status = match($code) {
    200 => 'OK',
    404 => 'Not Found',
    500 => 'Server Error',
};
  • Constructor Property Promotion – Less boilerplate in constructors.
  • Nullsafe Operator (?->) – Avoid null checks.
$name = $user?->name;
  • Attributes – Native annotations.
class PostsController
{
    #[Route("/api/posts/{id}", methods: ["GET"])]
    public function get($id) { /* ... */ }
}

PHP 8.1 – Modern Enhancements

  • Enums – Type-safe enumerations.
  • Readonly Properties – Immutable after initialization.
  • Fibers – Lightweight concurrency.
  • Intersection Types – Combine multiple interfaces.
  • never Return Type – For functions that never return.
enum Status {
    case Pending;
    case Approved;
    case Rejected;
}

PHP 8.2 – Type System Evolution

  • Disjunctive Normal Form Types.
  • Standalone true, false, null types.
  • New Random Extension.
  • Sensitive Parameter Attribute.
function login(#[SensitiveParameter] string $password) { /* ... */ }

PHP 8.3 – Developer Productivity

  • Deep Cloning of Readonly Properties.
  • json_validate() for quick JSON checks.
  • Explicit Typing of Class Constants.
if (json_validate($jsonString)) {
    echo "Valid JSON!";
}

PHP 8.4 – Property Hooks

  • Property Hooks – Inline getter/setter logic.
  • Improved Performance & Error Handling.
class User {
    public string $name {
        get => $this->name;
        set => $this->name = ucfirst($value);
    }
}

PHP 8.5 – Latest Innovations

  • Pipe Operator (|>) – Functional chaining.
  • URI Extension – Standards-compliant URL parsing.
  • Clone With – Update properties during cloning.
  • #[NoDiscard] Attribute – Warn if return value ignored.
  • Array Helpersarray_first(), array_last().
  • Persistent cURL Handles.
  • Better Debugging – Full stack traces on fatal errors.
$result = $data
    |> array_map('strtoupper', $data)
    |> array_filter($result, fn($item) => strlen($item) > 3);

Conclusion

PHP 8 has evolved into a robust, modern language with features that improve performance, developer experience, and security. Understanding these changes is crucial for interviews and real-world projects.