0

I am wondering how i can store my connection in a class variable and then keep reusing it ? Right now my code looks like this

This function sets up my Connection right now and is called everytime

function setDB()
{
$serviceAccount = ServiceAccount::fromJsonFile('firebase_credentials.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
->create();
$db = $firebase->getDatabase();

return $db;
}

This is one of my functions which needs the connection $db to get and update Data.

function Period($gameid, $action) 
{
$db = setDB();
$reference = $db->getReference('games/'.$gameid.'/Clock/CurrentPeriode');
$value = $reference->getValue();

if ($action =='m')
{
    $value = $value -1;
    $db->getReference('games/'.$gameid.'/Clock/CurrentPeriode')
    ->set($value);
} else {
    $value = $value +1;
    $db->getReference('games/'.$gameid.'/Clock/CurrentPeriode')
    ->set($value);
}
}
NoSoup4you
  • 640
  • 1
  • 10
  • 34

3 Answers3

1

The solution is using Singleton pattern:

class DbConn
{
    private $db;

    protected function  __construct()
    {
        $serviceAccount = ServiceAccount::fromJsonFile('firebase_credentials.json');
        $firebase = (new Factory)
            ->withServiceAccount($serviceAccount)
            ->create();
        $this->db = $firebase->getDatabase();
    }


    public function getInstance()
    {
        static $instance;

        if (!$instance) {
            $instance = new self();
        }

        return $instance;
    }

    public function getDb()
    {
        return $this->db;
    }

}

And usage will be looking like this:

function Period($gameid, $action) 
{
    $db = DbConn::getInstance()->getDb();
    $reference = .....
Lexxusss
  • 542
  • 4
  • 10
0

You can make setDB() into a singletron class and thus archive what you want.

0

Everyone here is saying use the singleton pattern. I'm going to say no don't use it as an answer: Read here and here.

Instead I would use a different approach: dependency injection. Use that paradigm and instantiate your classes upon initial load and then you use that to abstract your db layer from your logic making it easier to query rather than having to access that part every time you want to query or make changes like what you're doing now.

After all said and done, your logic should look something like this in the end:

// in your top configuration that gets loaded with your framework
// probably in it's own config file
$Factory = new Factory();
$serviceAccount = ServiceAccount::fromJsonFile('firebase_credentials.json');
$DbContext = new DbContext($Factory, $serviceAccount);

// somewhere in your app
$GamesRepository = new GamesRepository($DbContext);

// your logic in some function or part of the app
$gameId = 1;
$value = $GamesRepository->getCurrentPeriode($gameId);

$action == 'm' ? $value++ : $value--;
$GamesRepository->setCurrentPeriode($value, $gameId);

Because your repository is handling all the db conn stuff using another class that has the db connection handled from initial load of the page, you can just continue to use some kind of model or repository to fetch the info you need. I can expand on this but I think you should just think about your architecture a little and make a stab at it with what you know already if you decide the singleton pattern is not the best use case or want to try a different approach given it's downsides.

Paul Carlton
  • 2,785
  • 2
  • 24
  • 42