3

I have class which requires API Key (APIClent.php).

I want to initialise APIClient.php and share instance (singleton)

I have two controllers that need access to initialised instance (above).

Now each time I call my controller, it's getting new a instance of the class (APIClient) instead of getting an exist one if any.

How do I solve this? This is what my code looks.

AppServiceProvider.php

public function register()
{
    $this->app->singleton(APIClient::class, function()
    {
        return new APIClient(env('API_KEY'));
    });

}

ListController.php

public function __construct(APIClient $client)
{
    //does the same thing as below
   // $this->apiClient = App(APIClient::class);

   $this->apiClient = $client;
}

web.php

is just this line

$router->get('lists', ['uses' => 'ListController@index']);

Any tip or resources is appreciated.

Thanks

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • What do you mean by "each time I call the controller, its getting new a instance of the class"? Do you use the controller two times in one session? Singleton is single only in terms of one HTTP session. – Laraleg Mar 23 '18 at 07:14
  • so my objective was even if I refresh the page, I should still retrieve the old object and not create another. e.g I am using post man to get me all available lists and specific list. like so. localhost:8000/api/lists and and localhost:8000/api/list/4512 and both of these requests use the same controller. Am i going about this the wrong way? – user1594831 Mar 23 '18 at 07:20
  • 1
    Singleton is one and same instance per request, end of statement; 1 request equals = 1 "SEND" button in Postman. Also do not use `env('API_KEY')` within your code except in `config/*.php` . Your objective can be achieved by using cache or session only. Good luck. – Kyslik Mar 23 '18 at 08:32
  • Did you check `config/app.php` to see if `AppServiceProvider` is enabled or not? – haitran Mar 23 '18 at 08:40
  • Yes its enable and the class get loaded which means its working...I thought singletons instances can be used across the system. I will explore using session to overcome this issue. Thanks Kyslik – user1594831 Mar 23 '18 at 08:57
  • @Kyslik, it is you again :) , thanks for your advice, I put the similar answer before I saw your comment here. I'm not trying to steal your credit. – Yevgeniy Afanasyev Mar 04 '19 at 23:29

2 Answers2

3

Singleton does not mean that the object will stay in memory available for all client requests till the end of time. It merely means that server will create it only once ON EVERY REQUEST, and if during the same request the object will be needed again, it will use the created one.

Singleton may be different for different users, and may change on next request.

Your example does not demonstrate benefits of singleton, because you are using it in a __construct of a controller, and this function is also called only once on each request.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
-1
YOU write this : 

public function register()
{
    $this->app->singleton(APIClient::class, function()
    {
        return new APIClient(env('API_KEY'));
    });

}

each time, you call your `APICLient`, you create a new objet. 
If you want to use singleton pattern,
 just put the write code there without using `new` like 
 public function register()
{
    $this->app->singleton(APIClient::class, function()
    {
        return (env('API_KEY'));
    });
}
or Something like that.
Steve Ruben
  • 1,336
  • 11
  • 20
  • Thanks Steve. Even though I have solve my issue by other means, I still wanted to see if this would have worked for me, and so far no success. I think @kyslik already alluded to this and I am resigning to believe this is not possible with PHP after reading this article [http://blog.gordon-oheim.biz/2011-01-17-Why-Singletons-have-no-use-in-PHP/ (Singletons in PHP) – user1594831 Mar 28 '18 at 08:40