We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Is a singleton component in phalcon a real singleton?

Let me declare the question: As far as I know, when a request end, php will clear objects and other data that it created.

But as the Phalcon's document said: "Services can be registered as “shared” services this means that they always will act as singletons. Once the service is resolved for the first time the same instance of it is returned every time a consumer retrieve the service from the container".

<?php
//Register the session service as "always shared"
$di->set('session', function() {
    //...
}, true);

What I want to know is that: after a shared component was created, then at the next request, phalcon will reuse the shared component? I means phalcon will not create a new component instance.

https://stackoverflow.com/questions/29027142/is-a-singleton-component-in-phalcon-a-real-singleton

edited Mar '15

Your answer is yes.

When you use $di->setShared("key", "val"); or $di->set("key", "val", true) then $di will give you a single instance of key in a request lifetime that exactly mean singleton :)

a simple test:

$di->setShared('key', function () {
    $c = new \stdClass();
    $c->a = 1;
    return $c
});

// in your code 
$di->getShared('key')->a++;
$di->getShared('key')->a++;

echo $di->getShared('key')->a; // returns 3


1.3k

But what if between different request, does $di will give me the same object? I don't mean in one request lifetime.

Your answer is yes.

When you use $di->setShared("key", "val"); or $di->set("key", "val", true) then $di will give you a single instance of key in a request lifetime that exactly mean singleton :)

Singleton is valid in a request lifetime. from beginning to end. for example calling index.php till watching the output. it's a request lifetime.

I think you misunderstand a singleton functionality with a storage functionality that is same in all request like database.

But what if between different request, does $di will give me the same object? I don't mean in one request lifetime.

Your answer is yes.

When you use $di->setShared("key", "val"); or $di->set("key", "val", true) then $di will give you a single instance of key in a request lifetime that exactly mean singleton :)



1.3k

Ok, I get it. I just confuse because in java word, a singleton component means sharing in the lifetime of the server process. That's what I consider a real singleton.
Btw, does phalcon have any lazy-load strategy? I don't want to initialize every component in a request.

Singleton is valid in a request lifetime. from beginning to end. for example calling index.php till watching the output. it's a request lifetime.

I think you misunderstand a singleton functionality with a storage functionality that is same in all request like database.

But what if between different request, does $di will give me the same object? I don't mean in one request lifetime.

Your answer is yes.

When you use $di->setShared("key", "val"); or $di->set("key", "val", true) then $di will give you a single instance of key in a request lifetime that exactly mean singleton :)



10.5k
Accepted
answer

Yes, Phalcon DI completely supports lazy-loading and never initiate a class before it needed.

Ok, I get it. I just confuse because in java word, a singleton component means sharing in the lifetime of the server process. That's what I consider a real singleton.
Btw, does phalcon have any lazy-load strategy? I don't want to initialize every component in a request.

Singleton is valid in a request lifetime. from beginning to end. for example calling index.php till watching the output. it's a request lifetime.

I think you misunderstand a singleton functionality with a storage functionality that is same in all request like database.

But what if between different request, does $di will give me the same object? I don't mean in one request lifetime.

Your answer is yes.

When you use $di->setShared("key", "val"); or $di->set("key", "val", true) then $di will give you a single instance of key in a request lifetime that exactly mean singleton :)



1.3k

Thanks.

Yes, Phalcon DI completely supports lazy-loading and never initiate a class before it needed.

Ok, I get it. I just confuse because in java word, a singleton component means sharing in the lifetime of the server process. That's what I consider a real singleton.
Btw, does phalcon have any lazy-load strategy? I don't want to initialize every component in a request.

Singleton is valid in a request lifetime. from beginning to end. for example calling index.php till watching the output. it's a request lifetime.

I think you misunderstand a singleton functionality with a storage functionality that is same in all request like database.

But what if between different request, does $di will give me the same object? I don't mean in one request lifetime.

Your answer is yes.

When you use $di->setShared("key", "val"); or $di->set("key", "val", true) then $di will give you a single instance of key in a request lifetime that exactly mean singleton :)



7.9k

PHP itself is stateless, So every request is treated differently. You can story object in memory that can be used in different request, unless you use caching technology that serialize object and store it in file or database.

If you want real singleton object in PHP, you may consider using this https://appserver.io/