Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

I know how to program in PHP, but I don't know OOPS(Object Oriented Programming). Is it better to make themes in OOPS(Object Oriented Programming) or with normal coding? Is it better for performances, etc?

Thank you!

share|improve this question

marked as duplicate by Wyck, s_ha_dum, chrisguitarguy, anu, toscho Mar 5 '13 at 10:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Hi Maxwell, please read our faq, opinion questions like these are considered off-topic. –  Wyck Mar 5 '13 at 4:35
add comment

3 Answers 3

It will be better if you code theme in OOPS(Object Oriented Programming) because OOPS provides following benefits over normal/procedural oriented programming.

Benefits of Object Oriented Programming

  1. Modularity: The source code for a class can be written and maintained independently of the source code for other classes. Once created, an object can be easily passed around inside the system.

  2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.

  3. Code re-use: If a class already exists, you can use objects from that class in your program. This allows programmers to implement/test/debug complex, task-specific objects, which you can then use in your own code.

  4. Easy Debugging: If a particular object turns out to be a problem, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Cost of Object Oriented Programming

Object oriented design is complicated to do well, and a substantial amount of time is likely to be required to learn it in depth. If you have been developing procedural systems for some time then object oriented concepts will require learning a different way of thinking which is always challenging and requires effort.

share|improve this answer
add comment

To answer the question: No, you don't need OOP for WordPress themes.

This doesn't mean you can't do it, the vast majority of themes use some bits of classes, like extending the WP Menu Walker and some other small things, but I haven't seen yet a theme entirely developed in OOP.

Performance-wise – depending on the scenario – I doubt you would notice any substantial differences (especially with a caching system), so it's a matter of personal preference.

I've been considering the idea of developing themes in OOP, but that would be just for me to have more re-usable, better organized and easily extendable code.

However, as stated by @Wyck, this question is too generic to be answered properly.

share|improve this answer
add comment

Avoid OOP.

This is actually two questions in one

  1. should you use OOP methodology?
  2. should you use classes?

There is nothing wrong with using OOP, but most of the interaction which themes and plugins have with the wordpress core is very functional - on event "a" perform function "b". There is usually very little value in try to do Object Oriented Design (which you should always do before writing OOP code) to discover meaningful persistent classes of objects in your theme, as they are probably very trivial.

Should you still use classes in your code? depends on what kind of things you do. Some parts of wordpress core are designed to be extended by inheriting classes (widgets and walkers). It will be almost insane to try to avoid going OOP if you need to change/extend that functionality in your theme.

Some plugin writers use classes for data encapsulation/namespacing to avoid global variable and function names collisions, but this can be achieved also by prefixing "normal" function names.

There is no real performance problems with OOP relative to functional code. My main problem with OOP code is that for every non trivial class it is harder to follow the code when you try to read it.

To summarize, when you have the options to use both, the differences are more of a matter of personal preference for coding style then any major substantial difference. If you don't know OOP you should learn and decide by yourself which style suits you and the projects you do better.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.