Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the sense of naming invokables like classes?

I'm going trough ZfcUser to learn more about modules in Zend Framework 2. In the Module.php you can see

'invokables' => array(
  'ZfcUser\Authentication\Adapter\Db' => 'ZfcUser\Authentication\Adapter\Db',
  'ZfcUser\Authentication\Storage\Db' => 'ZfcUser\Authentication\Storage\Db',
  'ZfcUser\Form\Login'                => 'ZfcUser\Form\Login',
  'zfcuser_user_service'              => 'ZfcUser\Service\User',
  'zfcuser_register_form_hydrator'    => 'Zend\Stdlib\Hydrator\ClassMethods',
),

Now, what is the difference between giving classes a real abbr like "zfcuser_user_service" vs their fully qualified name? I assume, there is no technical difference, but maybe there is a convention how to name invokables, as ZF relies on them, how to name them?

like image 855
Maxim Zubarev Avatar asked Dec 11 '22 17:12

Maxim Zubarev


2 Answers

In contrary of the other answers, I can state that there IS a convention, which is to use fully qualified class names for service names wherever possible (and fake class names where the service is virtual and instance of another class).

If the problem is just about how ZfcUser does it, I can tell you that ZfcUser is not state of the art (currently 0.1.*) and needs a rewrite. You could look at BjyAuthorize for a better example.

I use either the FQCN (Fully Qualified Class Name) of the class that is defined as a service, or the FQCN of the interface it implements, which helps avoiding that the user consuming the service uses API not specified in the interface, and just in the implementing class.

Also, consider that it makes no real difference if you use \ or _ or lowercase or uppercase service names, since everything is normalized in the service manager. That basically means that zfcuser_service_user or ZfcUser\Service\User are the same.

To recap, here's a good practice you could follow:

'invokables' => array(
    // interface FQCN
    'Namespace\MyInterface' => 'Namespace\MyImplementation',

    // no interface available
    'Namespace\ClassName'   => 'Namespace\ClassName',

    // no interface nor own implementation available (similar to an alias)
    'Namespace\MyStuff'     => 'OtherNamespace\Stuff',
),

This is both easy to remember and allows end-users of your service to look for Namespace\MyInterface and find what they were looking for.

Use it also for factories and for services spawned from abstract classes if possible, since it makes things easier to remember for everyone.

like image 133
Ocramius Avatar answered Dec 14 '22 05:12

Ocramius


There is no convention about how aliases are named. You should only give your aliases a unique name so you don't get naming conflicts with other modules providing the same service. I use the FQCN in my project as well, because it avoids any conflicts 100% and it is very clear which class you are requesting from the service manager.

In the end it all comes down to personal taste, but I'd try to confirm yourself mostly to a single naming strategy and not mix up several tastes.

like image 39
Bram Gerritsen Avatar answered Dec 14 '22 06:12

Bram Gerritsen