title: Fatal error: Uncaught Error: Call to a member function getReference() on null in
body: <p>I am trying PHP to firebase insertion but I have got this error.</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>This is the code that I am trying to run-</p>
  
  <pre><code>class Account{
      protected $database;
      protected $dbname = "users";
  
      public function __construct(){
          $acc = ServiceAccount::fromJsonFile(__DIR__.'./firebase/key/makatravel2019-34ec2f4b7a9c.json');
  
          $firebase = (new Factory)
              -&gt;withServiceAccount($acc)
              -&gt;create();
  
          $database = $firebase-&gt;getDatabase();
      }
  
      public function insert(array $data){
          if(empty($data) || !isset($data)){
              return FALSE;
          }
  
          foreach($data as $key =&gt; $value) {
              $this-&gt;database-&gt;getReference()-&gt;getChild($this-&gt;dbname)-&gt;getChild($key)-&gt;set($value);
          }
  
          return TRUE;
      }
  }
  
  
  
  $users = new Account();
  
  var_dump($users -&gt;insert([
      '1' =&gt; 'John',
      '2' =&gt; 'Doe'
  ]));
  </code></pre>