Complex PHP apps have hundreds of services. Every service is associated with a service identifier, usually it is a string chosen by developer. For example, repository.users.sqlite
is an instance of UserRepository
class configured to work with users table, default sqlite instance.
What is the proper naming convention for such a string?
I want to avoid unnecessary typing, thinking, CR debates, copy/pasting the service name and searching the codebase to find out what sits behind the identifier.
I see some options, however none is perfect:
- use a string (something like
repository.users.sqlite
) and copy/paste it everywhere
$di['users.service'] = new UsersService($di['repository.users.sqlite']);
- create a constant to hold service identifier somewhere in service provider class and use it:
const USERS_REPOSITORY_SQLLITE = 'repository.users.sqlite';
const USERS_SERVICE = 'users.service';
$di[self::USERS_SERVICE] = new UsersService($di[self::USERS_REPOSITORY_SQLITE]);
- use built-in PHP class constants (
UserRepository::class
) as a naming approach for service identifiers. So, there is no need to create a constant or memorize something. Also, IDE's code completion works in this case.
$di[UsersService::class] = new UsersService($di[UsersRepository::class]); // default sqlite implementation is used here