gray and black galaxy wallpaper

Making an Apple Pie from Scratch (My Findings on Classes and OOP)

There’s a quote from Carl Sagan that goes “If you wish to make an apple pie from scratch, you must first invent the universe”. This is what I feel like I’m doing when I’m learning about classes in PHP. This is obviously me being overly dramatic, but I’m standing by my analogy. It’s also worth noting that I erroneously thought the quote was “If you want to create a toaster from scratch…” It’s going to be one of those days.

So why the drama about PHP classes? Well, I started working backward from “What is a PHP class?” and as it turns out, the PHP manual assumes you know what classes are in general. And what object-oriented programming is in general. Repeat as needed. So, it’s time to invent the universe.

What is Object Oriented Programming?

Object Oriented Programming (OOP) seeks to replace an older style of programming called “Procedural Programming”. In OOP, an Object refers to a thing that you want to store and process data about. Basically, an object could be anything as long as it has attributes (known as properties), and operations (things that can be done to, or performed by – typically referred to as methods). A common example of an object could be a person. Attributes (or properties) could be things like name, gender, date of birth, etc. Operations (or methods), could be actions like hire, promote, demote, etc. In this way, common properties and methods are associated with an object. In this example, the object would be a “Person”.

The Four Horsemen of the Apocalypse

The four horsemen of Object Oriented Programming are encapsulation, abstraction, inheritance, and polymorphism. God, I wish I was making this up. In a way, I’m glad I found these (and I found them everywhere, so this isn’t just a giant load of crap), because it means that my 9-minute introduction to classes (and in turn, OOP) was woefully underpowered. Going to be honest folks, this section is above my pay grade. For now, what I can tell you is these define the way data is stored and presented to the user – specifically as they pertain to classes and objects.

Objects and Classes

A class is a template used to create many Objects of the same type. Because of this, a class is sometimes referred to as a “type” (So meta!) Basically, think of classes as a template that allows you to create many objects from that template. IE: in my Person Object example from above, we can create many people from the Person class. It pains me that this example is so short, so I really want to flesh this one out a bit. The example that comes up often is a class could be compared to a cookie cutter, as once the cutter has been made, you can make as many of the same cookie as you want. (or car, or person, or tree, or fruit, or .. you get the idea)

Create Object -> Profit

So if I’m understanding this correctly, a class is used to define an object – then, when you interact with the object you can use keywords like $this if you’re in a method related to that object. Basically, every time you use $this, it goes ‘yep, we’re still talking about this object – ok let’s do this’. So $this is a catch-all for really any object currently being actioned. I’m actually glad $this came up – because it confirms what I suspected during my 12th day of code post.

In Conclusion…

I’ll be honest – I don’t think I know a lot more about PHP classes than I did before. I know what they are now, and I think I know when they’re appropriate, but I’m not ready to tackle them head-on. I’m going to finish this course with what I’ve learned about classes, and come back to them independently.

Author: Greg
Middle-aged father of two - still a kid at heart! Lover of all things geeky, trivia, factoids, craft beers, and procrastination. #SnorlaxIsMySpiritAnimal. #CanadianEh! "I don't know. Fly casual."

Leave a Reply