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);
}
}