Symfony2 service is not a single

I am building a simple pastebin web application on top of Symfony2, but I cannot make a global / singleton / "container scope". I am probably wrong.

The symfony2 service container add-on says that services are "created only once and the same instance is returned every time you request a service" but my service constructor is called on every request.

I can verify this quite easily from the logs. I just update /p/new

and I see another

[2012-03-31 21:32:56] app.INFO: InMemoryPasteService::__construct() [] []

      

I have also verified by logging the result

spl_object_hash($this->get('twobulb_paste_service'))

      

In controller (and hash is different for every request).

The environment (app / prod) doesn't seem to matter.

How to work with scopes says that scopes are "container" by default, so I suppose that means there should only be one instance of my service class.

I started with the standard (no vendor) Symfony distribution 2.0.12.

Source:

Possibly similar messages:

Any ideas?

+3


source to share


2 answers


Therefore, state is not persisted between requests. You can think of it as if the PHP interpreter is being reloaded between each request. This is exactly how PHP works.



+11


source


According to this post spl_object_hash

creates an md5 hash of the internal object pointer. So it's ok that it gives different hashes between requests. The hash does not change in the same request.



+2


source







All Articles