Skip to content

Update service_container.rst #13620

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

Merged
merged 1 commit into from
Jun 29, 2020
Merged

Update service_container.rst #13620

merged 1 commit into from
Jun 29, 2020

Conversation

zer0uno
Copy link
Contributor

@zer0uno zer0uno commented May 3, 2020

if a public service should not be fetched by $container->get(), how should it be fetched? The example shows the use of $container->get(), so I think there was a typo and it should be changed from "public" => "private"

@dbrumann
Copy link
Contributor

dbrumann commented May 4, 2020

Private services can not be fetched from the container, public ones can (but should not). I think the point is, that fetching any service from the container is regarded bad practice.

Instead this should probably point to injecting services and service subscribers & locators as recommended alternatives.

@zer0uno
Copy link
Contributor Author

zer0uno commented May 5, 2020

@dbrumann I understand and I agree you. So at this point I would say that the documentation should be a little bit clearer. It let me confused seeing in the example the use of $container->get() but then reading that it shouldn't be done. IMHO the sentence could be fine-tuned in this way:

From:

As a best practice, you should only create private services, which will happen
automatically. And also, you should not use the $container->get() method to
fetch public services.

To:

As a best practice, you should only create private services, which will happen
automatically. And also, despite the example shows it , you should not use the $container->get() method to fetch public services.

Comment on lines 853 to +863
As a best practice, you should only create *private* services, which will happen
automatically. And also, you should *not* use the ``$container->get()`` method to
fetch public services.
fetch private services.
Copy link
Contributor

@dbrumann dbrumann May 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

As a best practice, you should only create *private* services.
This allows for safe container optimizations, e.g. removing
unused services. You should not use ``$container->get()``
to fetch public services, as it will make it harder to make those
services private later. Instead consider :ref:`injecting services <_services-constructor-injection>`
or using :doc:`Service Subscribers or Locators </service_container/service_subscribers_locators>`.

I prefer this over the old text, because it not only tells you not to do it, but also why and what alternatives you have. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, really better especially because it tells you why you should do that.

if a public service should *not* be fetched by ``$container->get()``, how should it be fetched? The example shows the use of ``$container->get()``, so I think there was a typo and it should be changed from "public" => "private"
@javiereguiluz javiereguiluz changed the base branch from 5.0 to 4.4 June 29, 2020 15:59
@javiereguiluz javiereguiluz merged commit dd5e047 into symfony:4.4 Jun 29, 2020
@javiereguiluz
Copy link
Member

Thanks @zer0uno! I did the changes proposed by @dbrumann too while merging.

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

Successfully merging this pull request may close these issues.

4 participants