Best Practice for OOP in WordPress
I am currently digging deeper in more advanced WordPress development. I want to create something which is often refered to in the web as a theme framework which helps me to avoid implementing basic elements all over again. A very good example out there is the Hybrid Core Framework from Justin Tadlock. The reason why I want to create my own is because it helps me to learn more about the WordPress core.
Ok, coming to my question. About the structure of my framework: It consists of different modules seperated into different class files. The theme can now load these modules by using
add_theme_support()(which is a standard WordPress function). As an example I have created a module which makes it easy to create an options page for the theme in the WordPress backend:
What I don’t like is the mixture of logic and html output/callback. I would love to seperate the html callbacks
display*()from the rest of the code. I searched for design patterns and a very often used is the MVC or Model View Controler. Unfortunatly I am not sure, if this is what I am looking for, as I am not going to create different views on a model.
Another possibility I checked out was to create a new PHP class and encapsulate all the display functions into it with a static modifier. This brings me closer to what I wanted and works, but it leaves some misgivings, because I have no idea about performance issues nor about what is really going on behind the scenes (How is WordPress calling these static methods? Is it create an instance of that class?).
Most of my modules for the framework are going to use similar approaches; so this question is quite important for the overall layout, performance and “look&feel” of my framework.
Is someone out there with much more experience who could give me an example or some best practice guidlines on how to structure this framework using OOP and the WordPress hook and callback system?
Thanks for reading.
Greetings from Bremen (Germany),
- The topic ‘Best Practice for OOP in WordPress’ is closed to new replies.