Aware of class resolution syntax of PHP 5.5 for Laravel 5

by Pete Houston


According to the documentation of the latest release Laravel 5.0 (as of the time when this article is written), at http://laravel.com/docs/5.0#server-requirements.

It requires :

  • PHP >= 5.4

However, if you follow any tutorial or guide on Laravel 5 Events, you will see the event binding defined,

protected $listen = [
‘event.name’ => [
‘EventListener’,
],
UserWasRegistered::class => [
SendWelcomeRegistrationEmail::class,
SendUserVerificationEmail::class,
]
];

Be aware of the syntax ::class , which is the class name resolution syntax that is mentioned as new feature of PHP 5.5, http://php.net/manual/en/migration55.new-features.php

If you are, by accident, running a server with PHP < 5.5, the artisan command will bump with the following error.

[Symfony\Component\Debug\Exception\FatalErrorException]
syntax error, unexpected ‘class’ (T_CLASS), expecting identifier (T_STRING)

The solution is simple, instead of using ::class syntax, just define events as strings. That’s all you need to do. Do it your way.

For example:

‘user.registered’ => [
“App\Http\Controllers\SendWelcomeRegistrationEmail”,
“App\Http\Controllers\SendUserVerificationEmail”,
]

Probably, there are several ways to do it. I bet you can find more of it.

Anyway, have fun ! ☺

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.