'To call class only once in laravel

I have laravel application and I have used singleton pattern

public function boot/register()
    {
        $this->app->singleton(
            'className',
            new \App\Services\Api\Helpers\className
        );
    }

I want to call it once only when first time application bootstrap. It will store value and next time if I make api call, i don't want to call it

But with even singleton, it is calling everytime.

Can anyone help me?



Solution 1:[1]

Had to add answer as not got 50 rep yet, but..

Bhumi, the point of a singleton is not that it is only called once, it is that it is only constructed once and stored (Laravel singleton binding or older static variable method), much like putting something in cache.

If you want it to 'live' and be accessed, then it has to be stored, which if using the singleton binding it is.

Even if you used the older method of a static variable storing the instance, you still need a method to get the instance to retrieve the 'stored' instance, the Laravel container is just making things more convenient by handling the storage for you.

This explains more https://gist.github.com/davejamesmiller/bd857d9b0ac895df7604dd2e63b23afe#singletons

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron Appleton