I am trying PHP to firebase insertion but I have got this error.
Fatal error: Uncaught Error: Call to a member function getReference() on null in C:\xampp\htdocs\makatravel1\Account.php:44 Stack trace: #0 C:\xampp\htdocs\makatravel1\Account.php(72): Account->insert(Array) #1 {main} thrown in C:\xampp\htdocs\makatravel1\Account.php on line 44
This is the code that I am trying to run-
class Account{
protected $database;
protected $dbname = "users";
public function __construct(){
$acc = ServiceAccount::fromJsonFile(__DIR__.'./firebase/key/makatravel2019-34ec2f4b7a9c.json');
$firebase = (new Factory)
->withServiceAccount($acc)
->create();
$database = $firebase->getDatabase();
}
public function insert(array $data){
if(empty($data) || !isset($data)){
return FALSE;
}
foreach($data as $key => $value) {
$this->database->getReference()->getChild($this->dbname)->getChild($key)->set($value);
}
return TRUE;
}
}
$users = new Account();
var_dump($users ->insert([
'1' => 'John',
'2' => 'Doe'
]));