Title: Class function calling another class as global (wrong scope?)
Last modified: August 20, 2016

---

# Class function calling another class as global (wrong scope?)

 *  Resolved [e](https://wordpress.org/support/users/xaviesteve/)
 * (@xaviesteve)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/class-function-calling-another-class-as-global-wrong-scope/)
 * Hi guys,
 * I am trying to call a class function from another class function but when calling
   global $hcdb; it loads as NULL. I read some time ago about having to declare 
   it as a global but think it was only for variables. Any ideas? Sounds like quite
   a basic thing…
 * Stripped code of the files below:
 * **main.php** (the one that loads first)
 *     ```
       require_once(HC_PATH_INTERNAL."/includes/database.php");
       require_once(HC_PATH_INTERNAL."/includes/functions.php");
   
       $hc->format();
       ```
   
 * **database.php**
 *     ```
       class HcDatabase {
         function checknicename() { return 1; }
       }
       $hcdb = new HcDatabase;
       ```
   
 * **functions.php**
 *     ```
       class HcFunctions {
         function format() {
           global $hcdb;
           var_dump($hcdb); // returns NULL
           $hcdb->checknicename();
         }
       }
       $hc = new HcFunctions;
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [e](https://wordpress.org/support/users/xaviesteve/)
 * (@xaviesteve)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/class-function-calling-another-class-as-global-wrong-scope/#post-2221225)
 * The code belonged to a former developer, and wasn’t intended for WP. Just rewritten
   everything again to have a logical OOP structure that creates particular objects
   inside methods when needed, etc. Also adapted it to use functions so they are
   accessible through WP without hacks and everything keeps simplified, logical,
   scalable and secure

Viewing 1 replies (of 1 total)

The topic ‘Class function calling another class as global (wrong scope?)’ is closed
to new replies.

## Tags

 * [class](https://wordpress.org/support/topic-tag/class/)
 * [global](https://wordpress.org/support/topic-tag/global/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 1 participant
 * Last reply from: [e](https://wordpress.org/support/users/xaviesteve/)
 * Last activity: [14 years, 10 months ago](https://wordpress.org/support/topic/class-function-calling-another-class-as-global-wrong-scope/#post-2221225)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
