Fatal error: Call to a member function api() on a non-object-Collection of common programming errors

I’m running this code and I’m getting the following message:

“Fatal error: Call to a member function api() on a non-object in”

The line it refers to is the one just below where I write global $facebook. The facebook object is initialised in a config.php file which I include in my Database.php file, which I have linked in. I have researched this problem and it seems that it comes about when your $facebook is declared outside a function and you use it inside without making it global. As you can see, I have made it global, and I have even tried doing it without making it global – both result in that error.

Does anyone know why?

p.s. I don’t even see why it needs to be global since if config.php is included in my Database.php file, and that is included in the code below, then isn’t the $facebook object within the scope of this code?