Recent Posts


Modern PHP’s Reuse of Keywords


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

I was looking at some of the new features that are coming to PHP 8.1 and I saw enums were on the list. I also saw that PHP’s continuing its pattern of reusing keywords in different contexts to mean different things. An enum in PHP 8.1 looks like this

enum Status
    case DRAFT;
    case PUBLISHED;
    case ARCHIVED;

That’s the case keyword reused. Normally, case is used in a switch statment.

switch($foo) {
    case 'bar':
        // do something if $foo = 'bar'

However, if used inside an enum, case means something completely different.

You can see a similar pattern with PHP’s use keyword. In some contexts it means “import/aliase from a namespace”

use Some\Full\Class\Name\Foo;
$object = new Foo;

But in other contexts in means “apply the methods from this trait to this class”

class SomeClass {
    use SomeTrait;

This isn’t too big a deal for experienced programmers who know to look for it, but for inexperienced folks the cognitive overhead is a doozy. You thought you knew what use or case did, but all of a sudden you see it somewhere else while you’re also trying to learn a new thing. You’re left wondering if your original understanding was incorrect, or if there’s some connection between the old and new feature that you’re just not getting.

I’m sure there’s Very Good Reasons™ for this sort of thing, but it’s increasingly weird to see PHP switch sides in the boilerplate wars of the 1990s and early 2000s.

Copyright © Alan Storm 1975 – 2022 All Rights Reserved

Originally Posted: 8th November 2021