Nailing down some OOP basics

Posted by
on December 3, 2013

If you're new to Object Oriented Programming (OOP), then coding in Drupal 8 is going to feel plain weird. There's lots of new syntaxes and rules for accomplishing what seemed a lot simpler in Drupal 7. If you jump into learning OOP feeling resentful for all the weird stuff going on, then you might miss a chance at seeing the bigger picture.

There's definitely an OOP learning curve. But it's finite. Once you get to the top, you'll see a new way of coding that could actually keep you from ever coding yourself into a corner again. You know what I mean, that feeling you get where you don't want to touch any of the custom modules you've made because you're just not sure where the new code should go, and you know you need to refactor it, but there's never enough time and you feel like just starting from scratch? If you get OOP right, you can build more and more complicated projects without getting that feeling, for real. That's the payoff.

I've started to produce some videos to help future Drupal 8 programmers build up some OOP skills before they're forced to use them in new Drupal 8 projects. It's a lot more fun to work with Drupal 8 after you know what parts of what you're looking at are new OOP concepts and techniques.

We start by talking about the difference between "procedural" code and object-oriented code:

After walking through the code in an example of a simple procedural application, we then talk about how to derive objects from procedural code:

Once we get a sense of what a class might look like, we break it down into it's most common components, including methods, properties and constructors:

It seems like a little thing now, but the way you name your classes is quite important. Drupal 8 adopts some general standards around class naming conventions, and we talk about that briefly in this video:

While I'm going to go deeper into Object Oriented Programming and get into more complicated subjects like namespacing and autoloading, those videos aren't released just yet, so let's wrap it up by talking about how methods in a class are much more powerful than simple functions:

While this doesn't go incredibly far down the path of OOP mastery, hopefully it gives those of you who are new to OOP a sense of hope. The climb up the OOP learning curve will lead you to a lot of new freedoms when it comes to developing in Drupal or any other modern application.

Add comment

By submitting this form, you accept the Mollom privacy policy.