In the world of PHP, where constants are as steadfast as a developer’s love for coffee, PHP 8.3 has decided to give us a nifty little upgrade that could either be seen as a blessing or as PHP’s way of saying, “I told you so.” Enter the realm of Typed Class Constants, an upgrade that’s sure to make your code as robust as that double espresso you’re clutching.

The Wild West of PHP < 8.3

Let’s set the scene: It’s the good old days (or the chaotic ones, depending on how you look at it). You’re crafting an interface I with the innocent intention of declaring a constant PHP that lovingly whispers “PHP 8.2”.

interface I {
    // "PHP is always a string," they said. "It'll be fun," they said.
    const PHP = 'PHP 8.2';

Enter class Foo, striding in with the confidence of a cowboy at high noon, deciding, “Nah, const PHP? Definitely feels like an array day.”

class Foo implements I {
    // "Hold my beer," - Foo, probably.
    const PHP = [];

And there you have it, folks—chaos. But, fear not, for PHP 8.3 rides into town with a solution.

PHP 8.3: The New Sheriff in Town

With the introduction of PHP 8.3, the script has flipped. Typed Class Constants are here, bringing law and order to the wild, wild west of constant types.

interface I {
    // PHP 8.3, laying down the law
    const string PHP = 'PHP 8.3';

class Foo implements I {
    // Foo, trying to stir up trouble again
    const string PHP = [];
    // PHP 8.3: "I don't think so."

// Fatal error: Cannot use array as value for class constant Foo::PHP of type string

The era of “anything goes” is over. Now, declaring const string PHP = []; in class Foo will unleash a fatal error faster than you can say “type safety”.

Real-World Shenanigans

Let’s look at a couple of examples where Typed Class Constants can save your bacon or, at the very least, give you a good chuckle.

The Case of the Overenthusiastic Versioning

interface VersionControl {
    const string CURRENT_VERSION = '1.0.0';

class MyProject implements VersionControl {
    // "Version 2.0 will be revolutionary!" - A developer at 2 am
    const string CURRENT_VERSION = ['version' => '2.0.0']; // Oops!

Fatal error: “Developer’s dreams crushed by type safety. More at 11.”

The Misadventures of Data Types

interface Contract {
    const string SIGNATURE = 'John Doe';

class LegalDocument implements Contract {
    // "I thought signatures were supposed to be unique?" - Also a developer at 2 am
    const string SIGNATURE = 123456789; // A novel approach, indeed.

Fatal error: “PHP: ‘Nice try, but let’s stick to strings, shall we?’”

PHP 8.3’s Typed Class Constants might feel like PHP is tightening the reins, but it’s all in good faith. By ensuring that constants across implementations adhere to declared types, PHP is gently nudging us towards writing more predictable, bug-resistant code. So, the next time you find yourself declaring a constant, remember: PHP 8.3 has got your back, making sure your constants are as consistent as your need for caffeine.

And to all the developers out there, may your coffee be strong and your constants be correctly typed. Welcome to the new era of PHP constants.