In many themes I've seen (including TwentyEleven) and in the examples I've found online, when building the functions.php
file for a theme all functionality is declared in a global scope. To clarify, this is what a typical functions file looks like:
function my_theme_do_foo() { // ... }
function my_theme_do_bar() { // ... }
add_action( 'foo_hook', 'my_theme_do_foo' );
It would seem to me that things could be "encapsulated" a little better if a class was used:
class MyTheme {
function do_foo() { // ... }
function do_bar() { // ... }
}
$my_theme = new MyTheme();
add_action( 'foo_hook', array( &$my_theme, 'do_foo' ) );
The advantages of the second approach (in my humble eyes):
- Shorter function names
- Access to instance variables (the biggest advantage IMO)
- No global functions
The disadvantages:
- Classname could still cause conflicts
- Not as clear to "customize" with a child theme (would have to extend a parent class)
- Most themes haven't done it this way, so you'd be bucking the trend
I'm probably overlooking some things, but I am wondering why not take the OOP approach? It feels a bit "cleaner" to me, if anything. Perhaps I'm mistaken?
I'm fairly new to WordPress theme development, so forgive me if this is common knowledge in the WP community :). Just trying to learn why things are the way they are.