I just can’t stay away from this stuff. I told myself that I would convert this into a personal blog, but that I would try and keep the coding and such out of it. It would be a day-to-day account of my life. But damn. My life can get boring some days you know? Who want’s to read about me sitting around in my snuggie (just a snuggie) and reading for 6 hours? No one. That’s who. And old people I guess.. things like that would probably be exciting reading to them. And stalkers. But I’m sure I don’t have any of those. And even if I did, they’d probably already know that stuff.
Anyways, what I’m actually talking about today is Object Oriented Programming. To many it’s a very confusing concept and they’re flooded with questions such as when to use it, why would I use it, what IS it? And to others it comes as easy as breathing.
*NOTE* OOP is a slightly advanced concept. While you should try and learn it as soon as possible, you should have at least a moderate grasp of classes and functions.
What is it?
OOP is a way of programming that breaks down an object into pieces. Essentially an object would be something that you’re trying to achieve. OOP breaks this down so that you don’t have to continually write the same code. Instead, you can just copy over the class file, and then use the same functions as you normally would. That is the bonus of OOP.
To think of it another way, imagine a robot. That robot is what we are trying to build. But a robot is made of different pieces right? 2 arms, 2 feet, a body and a head. In Object Oriented Programming, you would take each piece and make it it’s own class.
How does it work?
What we would essentially do now, is populate this class with things that the arm can do. In this example, lets just make it very simple. All the arm can do is Judo chop and Punch. To accomplish this, all we would need to do is create two functions within our class. However, note that I said we have TWO arms. We would need some way of differentiating the right arm from the left right? The easiest way to do this, is to create a “constructor” function that would accept the name of the arm and store it for access later on.
name = $name; } function punch() { echo $this->name.' punches'; } function chop() { echo $this->name.' judo chops'; } } ?>
From that piece of code alone, we can actually create as many arms as we need. And that is the beauty of OOP. Now my robot can run on the same piece of code, regardless if it has 2 arms, or 50 arms. Imagine, an awesome 50 armed robot running off no extra code?
When would you use it?
As often as it makes sense. If you’re building any application, your best bet would be OOP. However, if you’re working on something like a website, there really isn’t a need for it. You’ll find people from all over the internet who say that you should use OOP all the time, and others who claim that it is the worst thing to happen to programming. Ignore them. In the end, it’s up to you to decide when you need to use OOP. If you find that you seem to be using a same bit of code all the time, wrap it in a function. If you find a group of functions you use all the time, and they’re related, wrap it in a class. If you have a group of classes you use all the time, wrap it in a framework.
For some reason there’s this song from the Wizard of Oz stuck in my head after reading that post. What could it be…? Ah yes, “If I only had a brain”
You and your 50 arm robot could sing it together. It’d be a riot
And yes, this is the calibre of comment you get from me when you make a joke mixed into a bunch of code -.-
(I can’t help it! The song’s stuck in my head now. I blame you…)
Ooh and your last post was a play off of “lions and tigers and bears” Oh my! So the theme fits
Lol. Class files. I see the beginning of feudalism and strata in coding. o.O
This post reminds me of a my few disastrous forays into Matlab rather a long time ago. I could not wrap my head around. It seemed simple enough, yet somehow when I felt I followed everything, and even on the occasions when every stage was correct and there really was NOTHING wrong with my code, it still wouldn’t work. And all we were trying to do was look at an object from different orientations in a picture… and it wouldn’t work for me!
My point is, this actually sounds like it would make life easier for people who know EXACTLY what they’re aiming for. OOP, in all it’s simplicity, would be easy to get caught up in… like when children learn how to use a comma properly for the first time and end up creating the most elaborate sentences that go on and on and on… and that’s when your English teacher breaks out the run-on sentences lesson and lets you know you’ve stepped over the line a tad. Like a fifty armed robot – when is that ever feasible? Or is it just that… we have the power to create and so let’s make as many fantastic and wonderful things possible..? “Go, go! Do it! NOW! Let’s create!” That sort of thing. I guess I’m just wondering what the downsides to OOP are… because it sounds too good to be true. Like Matlab, with it’s simplicity in using coordinate systems and matrices to move an object around and look at it from different views… it sounds so simple, yet half the time it wouldn’t work for no reason whatsoever… except apparently out of spite for me =P
N.B. Most of what I said about matlab comes from my use of it ages ago. This being said, I do not know how accurate or outdated my knowledge of it is.
You’re right, and I think one of the biggest downsides of OOP is it’s re-usability. Once it’s written, people can ignore it. And then they forget about it. And soon they’re trying to force the application to work in ways it was never intended and it takes huge performance hits.
Take for example your 50 armed robot. Instead of 50 objects, each containing arms, why not modify it so that upon creation you set the number of arms, which simply creates an array holding the information about each arm? That way, you only need to create one object.