samedi 21 mars 2015

How to Keep a Single Instance of class in PHP(Laravel 5) for all the incoming connections to my site?

I am working on a Laravel 5 based web application, I have a class "Connection" that is implemented as a Singleton



<?php

class Connection{

private static $instance = NULL;
private static $connection = NULL;

private function __construct(){
self::connection=API::openConnection(); //just an example of connection (from other class)
}

public function getInstance(){


if(NULL==self::$instance)
{
self::$instance=new Connection;
}

return self::$instance;

}


Now the problem I am facing is that, Whenever a connection fetches the instance and instantiates it for first time and opens the connection, but when other user visits the same page (earlier user visited), a connection is reopened(i.e. the class is instantiated again), The openConnection() can give only one connection at a time and the previous one closes when new user opens connection.


Is there any solution that I can use the single connection for multiple user requests?


Note:- Above code is just an abstraction of real problem, to get an idea.


Thanks for any help.





Aucun commentaire:

Enregistrer un commentaire