Title: instatiate functions.php class
Last modified: August 19, 2016

---

# instatiate functions.php class

 *  [pudde](https://wordpress.org/support/users/pudde/)
 * (@pudde)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/instatiate-functionsphp-class/)
 * yo,
 * i’ve decided to try and wrap my functions.php in a class (i remember talking 
   to some clever guys at wcnyc about it).
 * now, but where would i instantiate this class? if i instantantiate in header.
   php, i can only access the object in there. ideally i’d like to instantiate it
   for the entire theme.
 * am i making any sense at all here or have i gotten i all wrong?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/instatiate-functionsphp-class/#post-1305460)
 * Converting your functions to methods of an object is a great idea! I do all of
   my WordPress coding this way now-a-days. Here’s what I have found to work:
 * **functions.php**: instantiate here.
 *     ```
       $my_class = new $my_class();
   
       class my_class{
           // class stuff
       }
       ```
   
 * The object `$my_class` is now available to be used in most template files save
   _header.php_, _sidebar.php_ and _footer.php_ due to the fact that your theme 
   is (most likely) using the following _template tags_ to include these files: `
   get_header()`, `get_sidebar()` and `get_footer()`.
 * When a function includes a file, that file inherits the scope of the function,
   therefore `$my_class` is not defined in _header.php_, _sidebar.php_ and _footer.
   php_.
 * The easiest way around this is to declare `$my_class` as global at the start 
   of each included template file that is accessed via a function.
 * This drove me **CRAZY** until I read the [php documentation regarding variable scope](http://php.net/manual/en/language.variables.scope.php).
 * Best wishes,
    -Mike
 *  Thread Starter [pudde](https://wordpress.org/support/users/pudde/)
 * (@pudde)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/instatiate-functionsphp-class/#post-1305495)
 * there we go! i had it all right, except i hadn’t declared my_class as global 
   in all the template files. thanks, all works really well now!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘instatiate functions.php class’ is closed to new replies.

## Tags

 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [oop](https://wordpress.org/support/topic-tag/oop/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 2 replies
 * 2 participants
 * Last reply from: [pudde](https://wordpress.org/support/users/pudde/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/instatiate-functionsphp-class/#post-1305495)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
