General

What's New in PHP 8

9 dk okuma

PHP 8 is the most comprehensive update to the language in the last 15 years. Released in November 2020, this major version brought PHP up to modern language standards with features like a JIT compiler, named arguments, match expressions, union types, the nullsafe operator, and attributes. The PHP 8.1, 8.2, and 8.3 updates have further strengthened the foundation. If you're still on PHP 7.x, you should consider migrating to PHP 8 — not just to "keep up with the times," but for performance, security, and maintainability.

In this article, we'll explore the most important features introduced in PHP 8 with code examples, and clearly show what each feature does in real-world scenarios.

1. JIT (Just-In-Time) Compiler

The most talked-about feature in PHP 8 is the JIT compiler. Traditionally, PHP code was first converted to opcodes and then interpreted by the Zend VM. JIT changes this: code is compiled directly into x86 machine code and executed on the CPU.

Practical impact: It provides noticeable speed gains in math-heavy operations, ML-like computations, and loops. However, in most web applications (CRUD-focused, I/O-heavy), the impact is limited because the bottleneck is the database or network — not the CPU. For those who want to use PHP for game engines or image processing without C extensions, it's nothing short of a revolution.

JIT is disabled by default. To enable it, you need to configure the opcache.jit_buffer_size and opcache.jit settings in php.ini.

2. Named Arguments

A feature that revolutionizes code readability and flexibility in functions with many parameters:

// PHP 7 and earlier
htmlspecialchars($string, ENT_QUOTES | ENT_HTML5, 'UTF-8', false);

// PHP 8
htmlspecialchars($string, double_encode: false);

You only pass the parameter you want to change; the rest stay at their default values. You can also change the order of parameters. This feature significantly improves code quality, especially in library functions with many parameters and in config arrays.

3. Constructor Property Promotion

A clean feature that eliminates the repetition we encounter every day when writing OOP code:

// PHP 7
class Point {
    public float $x;
    public float $y;
    public float $z;

    public function __construct(
        float $x = 0.0,
        float $y = 0.0,
        float $z = 0.0
    ) {
        $this->x = $x;
        $this->y = $y;
        $this->z = $z;
    }
}

// PHP 8
class Point {
    public function __construct(
        public float $x = 0.0,
        public float $y = 0.0,
        public float $z = 0.0,
    ) {}
}

DTOs (Data Transfer Objects), Value Objects, and simple models now fit into 3–4 lines. The burden of writing boilerplate code is significantly reduced.

4. Match Expression

The modern, safe successor to the classic switch statement. It has three key differences: it uses strict (===) comparison, it returns a value, and it doesn't require break statements:

// PHP 7 - switch
$message = '';
switch ($status) {
    case 200:
    case 201:
        $message = 'Success';
        break;
    case 404:
        $message = 'Not Found';
        break;
    case 500:
        $message = 'Server Error';
        break;
    default:
        $message = 'Unknown';
}

// PHP 8 - match
$message = match($status) {
    200, 201 => 'Success',
    404 => 'Not Found',
    500 => 'Server Error',
    default => 'Unknown',
};

It's shorter, safer (strict type comparison), and eliminates fall-through bugs caused by forgotten break statements.

5. Nullsafe Operator (?->)

The end of chained null checks:

// PHP 7
$country = null;
if ($user !== null) {
    $address = $user->getAddress();
    if ($address !== null) {
        $country = $address->getCountry();
    }
}

// PHP 8
$country = $user?->getAddress()?->getCountry();

If the chain returns null at any point, the expression safely resolves to null without throwing an error. This is a lifesaver when dealing with missing relationships while using Doctrine, Eloquent, or other ORMs.

6. Union Types

A parameter or return value can now accept more than one type:

// PHP 8
function formatId(int|string $id): string {
    return (string) $id;
}

function findUser(int $id): User|null {
    // ...
}

Previously, these situations required writing @param int|string in PHPDoc and then doing manual type checks at runtime. Now it's handled at the language level. In PHP 8.0, null is written as a union type (User|null); in PHP 8.1+, the ?User shorthand is also valid.

7. Attributes (Annotations)

The annotation/attribute system familiar from Java and C# arrived in PHP 8. Metadata that used to be written in DocBlock comments is now at the language level:

// PHP 7 - PHPDoc annotation
/**
 * @Route("/api/posts/{id}", methods={"GET"})
 */
public function get($id) { /* ... */ }

// PHP 8 - native attribute
#[Route('/api/posts/{id}', methods: ['GET'])]
public function get($id) { /* ... */ }

All major frameworks including Symfony, Doctrine, and Laravel have migrated to the attribute system. Static analysis tools can now read metadata directly via PHP's reflection API — no string parsing required.

8. Enums Introduced in PHP 8.1

PHP 8.1 brought native enum support to the language (in PHP 8.0, fake enums were still being written using class constants):

enum OrderStatus: string {
    case Pending = 'pending';
    case Approved = 'approved';
    case Shipped = 'shipped';
    case Delivered = 'delivered';
    case Cancelled = 'cancelled';

    public function label(): string {
        return match($this) {
            self::Pending => 'Pending',
            self::Approved => 'Approved',
            self::Shipped => 'Shipped',
            self::Delivered => 'Delivered',
            self::Cancelled => 'Cancelled',
        };
    }
}

Type-safe enums that can have methods defined and implement interfaces — they make domain modeling in large projects much cleaner.

9. Readonly Properties (PHP 8.1)

A property becomes immutable once it has been set. This supports writing immutable Value Objects at the language level:

class Money {
    public function __construct(
        public readonly int $amount,
        public readonly string $currency,
    ) {}
}

$price = new Money(100, 'TRY');
$price->amount = 200; // Error: Cannot modify readonly property

PHP 8.2 also introduced readonly class — making all properties of a class readonly in a single line.

10. Performance and Type System Improvements

Improvements that aren't visible in the code but are felt in every application:

  • str_contains(), str_starts_with(), str_ends_with(): Finally, native string functions (PHP 8.0).
  • Smarter type comparison: 0 == 'foo' now returns false (previously it returned true, which was a major source of bugs).
  • More consistent error messages: Internal functions now throw TypeError instead of Warning, so errors are caught earlier.
  • Weak Maps: A structure that can store object references without the risk of memory leaks.
  • Performance: Generally 10–20% faster than PHP 7 (even without JIT), with lower memory usage.

Migrating to PHP 8 — What to Watch Out For

Things you'll encounter when migrating an existing PHP 7 project to PHP 8:

  • Deprecated features: Some older features were removed or now produce errors in PHP 8. For example, writing to $GLOBALS is now restricted.
  • Type comparison has changed: Number-string comparisons are now stricter, which may expose hidden bugs in existing code.
  • Internal function signatures: Some function signatures have been tightened. strpos no longer accepts null for haystack/needle.
  • Third-party libraries: Verify that the packages listed in your composer.json support PHP 8.
  • WordPress projects: WordPress 6.6+ supports PHP 8.0–8.3, but some plugins may still be incompatible.

Before migrating, we recommend using automated refactoring tools like Rector and running static analysis with PHPStan.

Conclusion: PHP 8 Is a Modern Language

PHP 8 is the best possible answer to anyone who says "PHP is outdated." JIT, named arguments, match expressions, enums, readonly properties, attributes — all of these are features that modern languages have offered for years, and they're now in PHP too. What's more, the PHP 8.x release series adds new features every year; PHP 8.3 brought typed class constants, json_validate(), and dynamic class constant fetch.

If you still have a project running on PHP 7.x, you should consider upgrading for security reasons alone — support for PHP 7.4 ended entirely in November 2022. If you're starting a new project, begin with PHP 8.3 or 8.4.

As YazılımPark, we build modern, high-performance, and secure custom software solutions with PHP 8. If you need a corporate CRM, dealer panel, appointment system, or API integration, get in touch with us.

Get a PHP Custom Software Quote

Looking for consulting on PHP migration or custom software? Reach our experts via WhatsApp or phone.

İşletmeniz İçin Doğru Çözümü Birlikte Bulalım

Web sitesi, hosting, SEO veya chatbot ihtiyaçlarınız için size özel bir teklif hazırlayalım.