Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The container screencast should not say that you can use anything as id #5

Open
stof opened this issue May 6, 2015 · 5 comments
Open

Comments

@stof
Copy link

stof commented May 6, 2015

Ids are always lowercase in the container. So if you try to set logger and then LOGGER later, it will actually overwrite the previous service.
Thus, Symfony optimizes the code path to retrieve services with their lowercase id (some improvements have been made, and others are still pending in PRs at this time). so using a non-lowercase id when calling get() will give you a performance penalty (which is not negligeable in case you retrieve lots of services. Ask the Drupal guys who asked for and contributed some of these optimizations)

Thus, using special chars will create issues once you start defining definitions and dumping the compiled container to PHP. The only valid characters when dumping containers are chars allowed in PHP identifiers (letters, digits and some unicode chars), underscores, dots and backslashes (with some conflicts in case you have 2 service ids differing only by a backslash vs a dot in the same place btw, or by an underscore vs no char).
This is why the Symfony best practices are to use dot-separated snake case parts when builing the id.

@weaverryan
Copy link
Member

@stof WOW. I actually didn't know about the optimizations - that's really sweet :). I've added a note at sha: 897b006 and @Leannapelham is encoding that note into the video.

Thanks - I try to be a bit more casual in the screencasts, but not with the intention to be incorrect :).

@stof
Copy link
Author

stof commented May 6, 2015

@weaverryan the fact that ids are case insensitive is not an optimization. It should be explained explicitly IMO (it is a big difference with array keys)

@weaverryan
Copy link
Member

@stof So the key points are that:

A) Some characters are not valid (I think I've covered that with the note - i.e. stick to the standard, or else risk hitting some invalid chars)
B) The service ids are case-insensitive, but it's faster if you call get() with a lowercased string (and so, why not be consistent and call set() with a lowercased string too, even though that doesn't matter).

Does that sum it up? If so, I'll get a note that suits this - I want it to be as short as possible, but this is a more advanced tutorial, so it fits.

Thanks!

@weaverryan weaverryan reopened this May 6, 2015
@stof
Copy link
Author

stof commented May 9, 2015

yes, this is OK.

@weaverryan
Copy link
Member

Thanks - I've updated the note once more - hopefully it's good now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants