I’m doing a heavy rewrite of my main WordPress theme that all of my other themes are based on. As someone who has spent a lot of time writing code, one of the things that has been bugging me about WordPress themes is all the duplication of HTML.
For instance, if all your pages have a format involving a header element, a body element, and a footer element, then you have to duplicate that HTML in single.php, page.php, 404.php, etc. Then if you ever change it you have to edit every one of those files just to make the same changes over and over again. You’re probably going to forget to do it in some files and you’re going to do it wrong in others.
Wouldn’t it be great if you could just call a function that would establish this pattern for you in all files? Then if you needed to change it, you would only have to change it in one place and all your site’s pages would be effected. But what if you need to tweak the template slightly for different pages? This is exactly why object oriented design is important.
So you have a base class that draws the usual page structure and then provide override-able and abstract methods so that a new class can be created if you need to customize. It works perfectly because it gives you maximum code reuse and there’s no duplication.
However, it does make the theme very code heavy. There’s not a ton of code, but enough that would intimidate most people. Plus, if you don’t have a lot of experience with object oriented design getting the extensibility right is going to be a bit difficult.
But that’s kind of my point for this post. It’s not necessarily that you need to do this, because obviously themes can be made without it. But it’s the fact that object oriented design is important to web applications and even blogs too. If you do a lot of PHP coding but aren’t yet familiar with the concepts behind object oriented programming, then learning more about that will really be a benefit to you.
Submit a Comment