• 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)
  • 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.

    Best wishes,
    -Mike

    Thread Starter pudde

    (@pudde)

    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.