PHP Development

PHP 8.3 in Production: Typed Constants, json_validate(), and More

PHP 8.3, released December 2023, is the third iteration of the PHP 8.x series. It builds on the strong foundations of 8.0, 8.1, and 8.2 with pragmatic additions: typed class constants close a long-standing type-safety gap, json_validate() prevents the waste of fully decoding JSON just to check its validity, and several internal improvements make the engine faster.

Typed Class Constants

Before PHP 8.3, class constants had no type enforcement — any value could be assigned regardless of what the docblock claimed. Now you can declare the type directly:

interface HasVersion {
    const string VERSION = '1.0.0'; // type-enforced
}

class App implements HasVersion {
    const string VERSION = '2.0.0'; // valid — string

    // const string VERSION = 42; // Fatal error — not a string
}

// Also works with union types
class Config {
    const int|string DEBUG = 1;
}

json_validate(): Efficient JSON Validation

Previously, the only way to check if a string was valid JSON was to decode it and check for errors — allocating memory for the full decoded structure even if you only needed a yes/no answer:

// Before PHP 8.3 — wasteful
$isValid = json_decode($payload) !== null && json_last_error() === JSON_ERROR_NONE;

// PHP 8.3 — validates without decoding
if (json_validate($payload)) {
    $data = json_decode($payload, true);
}

Readonly Property Improvements

PHP 8.3 allows readonly properties to be re-initialised during cloning via __clone() — enabling immutable value objects to be cloned with modified properties without ugly workarounds:

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

    public function withAmount(int $amount): self {
        $clone = clone $this;
        $clone->amount = $amount; // allowed in __clone context
        return $clone;
    }
}

gc_status(): Garbage Collector Insight

gc_status() now returns richer information about the garbage collector, including the number of roots, running state, and buffer size — useful for profiling long-running PHP processes like queues and daemons.

Randomizer Additions

The RandomRandomizer class added in PHP 8.2 gains two new methods:

  • getBytesFromString(string, length) — cryptographically random characters from a given alphabet (perfect for tokens and slugs)
  • nextFloat() — uniform float in [0.0, 1.0)

Performance

PHP 8.3’s internal improvements yield a 5-10% throughput improvement in typical web workloads compared to PHP 8.2, primarily from reduced memory allocations in hot interpreter paths.

PHP 8.3 is a solid production upgrade. The typed constants feature alone eliminates a category of runtime errors in large codebases. If you are on PHP 8.2, upgrade to 8.3 — the migration is smooth and the gains are real.

Share this post:
Copied!

Leave a Comment

Your email will not be published.

READY TO BUILD?

Let's Build Something
Amazing Together

Tell us about your project. We'll have a proposal in your inbox within 24 hours.

Free Consultation
NDA Available
Fixed-price Options
Dedicated PM