Tag Archives: Object-oriented Programming

Polymorphism and WordPress: Interfaces

Let’s talk about interfaces. As a WordPress developer, how can they be useful to you and your projects? It’s going to be a tough sell because WordPress core doesn’t use them and we’ll see why this is an issue a bit later. That said, you’ll still find this article useful if you’re looking to:

  • Learn more about PHP and not just WordPress
  • Build strong and extensible PHP code
  • Reduce bugs in your open source plugins
  • Use open source PHP frameworks

Like the article on abstract classes, you’ll get a detailed example to help you with the topic. It’ll explain how interfaces work and how you can use them. You’ll also get a good idea of the design decisions that warrant the use of an interface.

Continue reading Polymorphism and WordPress: Interfaces

Polymorphism and WordPress: Abstract classes

When we covered inheritance, there were some questions about interfaces and abstract classes.

  • What can you use them for?
  • What are the advantages?
  • When should you use them?

These are all great questions that are worth exploring. As the title suggests, “Polymorphism” is the object-oriented feature that helps answer these questions. It’s not an easy feature to grasp.

That’s why most of the article will be about an in-depth example. You’ll see the thought process involved with using it. This will help you understand it better so you can apply it to your own projects.

Continue reading Polymorphism and WordPress: Abstract classes

Using inheritance with WordPress

As a WordPress developer, you’re always looking for ways to better reuse your code between projects. Your time is valuable and you don’t want to reinvent the wheel each time you start a new project.

Object-oriented programming can help you with that. In a previous post, I covered why you should learn it. Now it’s time to take things further by going over the main feature for code reuse. You’ve probably heard about it before. It’s called “inheritance“.

Continue reading Using inheritance with WordPress

The first thing you should learn from object-oriented programming

You’ve decided to learn object-oriented programming, but you don’t know where to start. Object-oriented programming has so many concepts and features. The whole thing can feel overwhelming at times. Let’s help you get started on the right foot.

What’s a great place to start? With the feature that you’ll associate the most with object-oriented programming. It’s called encapsulation. Most modern programming languages support encapsulation using classes.

Continue reading The first thing you should learn from object-oriented programming

Why object-oriented programming is your next step as a WordPress developer

You’re a WordPress developer. You might have a few plugins under your belt or a theme or two. You build WordPress sites for clients or just for yourself. You’ve heard of object-oriented programming, but, each time you look into it, it makes no sense!

You tell yourself object-oriented programming isn’t useful or worth the trouble. The goal of this article is to focus on explaining this value to you. At the end of this article, you should have a clear understanding of why you should learn it.

Continue reading Why object-oriented programming is your next step as a WordPress developer

Single responsibility principle, WordPress and You

For WordPress developers, it can be hard to improve your PHP skills. You lack the resources or tutorials to drive home these concepts. This happened just a few weeks ago when Nathaniel asked for help with his Stack Overflow question.

He was looking for help applying the single responsibility principle with WordPress. I sent him a bunch of replies but closed off saying I needed to write a post about it. This article is a detailed explanation of my thoughts following that conversation.

As WordPress developer, you might have started working with or looking into object-oriented programming. Maybe you even heard of something called SOLID.

As a whole, SOLID can be an intimidating topic. But the single responsibility principle is just one part of it (it’s the ‘S’ in SOLID). That makes it a more manageable topic to discuss and help you with.

At the end of this article, you should have a better understanding of it. You can also use the provided code samples in your own plugins and themes.

Continue reading Single responsibility principle, WordPress and You

WordPress core as a gateway to better coding

How many of you got into PHP coding because of WordPress?

– Matt Mullenweg

Me and roughly half the room of 200+ attendees raised their hand to that question. That’s pretty incredible mused Matt. He asked that question while answering the larger question asked by 9th grader Lucas Cherkewski which was “How can WordPress be used in an education context to learn coding?”

I have been involved with WordPress for over half its 10 year existence and while I have gone from simple attendee to volunteer to community organizer over that time. I have always been a bit of the crazy one in terms of trying to help the community code better and learn about more advanced programming topics. That’s because I have been coding since I am 7-8 years old so while I learned PHP through WordPress. I had done plenty of programming before then.

Plugin development is like high school

When I was coding in high school, I couldn’t even do something as cool as a WordPress plugin. The web was just starting. I was mostly coding in Basic doing silly things like a text-based RPGs. So I think it’s amazing that someone can solve small problems with a plugin and share it with the world!

For a lot of people, that’s enough. They can solve small problems without knowing a lot of advanced programming concepts. They don’t need to graduate to college level concepts. WordPress has a lot of great APIs that are easy to use and require no advance programming knowledge.

How do you graduate to college level coding?

I did a WordPress meetup on object-oriented PHP recently and I can’t say that I am the best of teachers and a lot of the utility and reason to use object-oriented programming was lost on most because WordPress doesn’t use a lot of objects yet due to its recent PHP4 heritage.

How do I get better at PHP coding?

I don’t get asked that question a lot, but, a few months ago, Michal Bluma asked me how I got into more advanced PHP coding. I told him definitely not with WordPress. I told him I had done a lot of learning looking at the code base Doctrine1, but version1 of Doctrine is mostly discontinued and Doctrine2 is a different beast entirely.

I told him take a look at Symfony, but that’s akin to someone going from high school to college graduate. The knowledge gap is HUGE. The same would be said about Doctrine2, Zend Framework, etc.

Fundamentally, there’s no way easy way for a WordPress developer close the gap.

WordPress core is an ideal candidate for this

WordPress core would be a great place for people to get a taste for advanced programming knowledge. What’s great is that all the APIs already hide most of the internals so, if someone is looking at the core code, it’s because they want to look at it. There’s an opportunity here to allow them to become better coders! Unfortunately, that opportunity is currently wasted.

Core needs stronger coding standard

I am not talking about syntax here, but actual code quality. Core devs need to raise the bar a bit more. I am not talking about super advanced things like Reflection objects or metadata objects, but just better knowledge and use of object-oriented principles. Learning how to do object-oriented programming is really the next step if you want to learn more.

As an example, last night I submitted a ticket to core about the use of final with the WP_Post class. In my opinion, that should have never made it to core in the first place and shows a fundamental misunderstanding of object-oriented coding especially in an open source environment.

What needs to change

Honestly, it would be good if WordPress core at least used some PHP object-oriented best practices. Here are things that would be great for people looking to learn a bit more about being better PHP programmers:

  • Using interfaces where it makes sense to do so like with WP_Widget and WP_Post for example.
  • Severely limit the use of magic methods especially __get and __set
  • Exception handling instead of the white screen of death
  • Proper variable visibility
  • More abstract classes

This is just to name a few. You can find some of these in the code base already, but new code is still being committed where these basic rules are disregarded which is really the point I am trying to get across.

As a core dev, you should be holding yourself up to higher standards because you are indirectly a teacher for thousands of fledgling PHP developpers.

Feel free to leave a comment below or discuss on Hacker News.