Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

austenc's avatar

Using ::class not working with php 5.4.4?

I use the ::class property (is that what this is called in php lingo?) in various places in my code to resolve the fully namespaced path easier (such as in relations).

Everything works fine on my localhost (using homestead), but when i deploy to a remote server (php 5.4.4 on it), I'm getting this error:

syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

Here's the snippet of code that's causing the issue:

$repo = App::make(UserRepository::class);

Does anyone have any idea why this wouldn't be working? It's my understanding that ::class should work in php5.4+?

0 likes
7 replies
freekmurze's avatar
Level 13

Nope, it's only available in 5.5 and above

1 like
austenc's avatar

@freekmurze -- awesome, that would explain it... thanks! Any chance you could link me to the php docs that go over this ::class thing? Not having much luck finding it.

austenc's avatar

Thanks, my google searches didn't turn up much. Was wondering where in the docs it was. Cheers!

xingfucoder's avatar

Hi, if you are using the php5.4 version maybe you could emulate the class static function with some code as follow:

Step 1. Create a trait called as you desire, in example:

    namespace YourProjectNamespace;
    trait ClassEmulationTrait {
        static function class(){
                return __CLASS__;
        }
    }

Step 2. Use this trait within each class you need to emulate the ::class method.

    namespace YourProjectNamespace;
    class UserRepository{
        use ClassEmulationTrait;
    }

Later in your code you could use:

     echo YourProjectNamespace\UserRepository::class();

You will need to import all namespaces used, and remember if you update PHP to > 5.5 version you need to update your code removing all that trait use.

Try and comment us.

Please or to participate in this conversation.