| Object Oriented Programming |
|
|
|
| PHP - Intermediate | |||||||||
| Written by Stuart Duncan | |||||||||
| Friday, 27 November 2009 17:32 | |||||||||
|
Object Oriented Programming is usually used the kind of programming that you find in video games, robotic engineering... even desktop interfaces... so it's hard to imagine how you could use something like an 'object' when working with a website but the truth is that there are a lot of advantages to it. By learning how they work, hopefully you'll learn how and why they should be used in your code. Objects are pretty much exactly as you know them today, they are things... items.... stuff. A ball is an object, a house is an object, a car is an object... even a person. To transfer this way of thinking to your code, you can imagine your users as objects, databases as objects and everything else. To help you envision this a little clearer, I'm going to use the example of a dating site. In it, you have multiple people looking for each other using compatible criteria. 1 person finding 1 person... or.. 1 object finding 1 object. You have to remember that each person is just that... a person, their differences break down from there... gender, hair colour, likes and dislikes. So you can say that each person is an object and in this way, all of your code to manage each person only need be written once, not twice. Ok, enough explaining that a person is an object, let's dive into the code:
And there you have a person object... complete with a "constructor" which is basically a bunch of code that gets run as soon as you create the object. And to create that object, you put this into your code somewhere:
And there you have a reference to a person. I know you're thinking, what's the point of that? Well, we'll get to that. But first, imagine that those 2 people from before are different but are both 'objects'... well, we can easily create two people like this:
And there we have two people. From there, we can begin to build out their differences. Let's go back to out object (called a class, if you noticed in the code) and give our object some properties.
Now we can see that our person has properties that can be set, changed and read so that we can compare them.
You'll notice that an object is given a name... $personA and $personB in these cases. From there, you can reference each person separately using those names. Also, you can see that to reference a piece of information about that object, you use '->'. This tells PHP that you want to go into that object and look for that property (or anything else in the object). So you can see how our personA and personB have different properties... this means that we can compare them on the page of our dating site and see if they're compatible! Now, this is where I used to wonder, what's the point of setting things on the page and reading them again further down, just compare them.... well, this example is done only as a demonstration, not as a real world example. In the real world, you'd likely have an ID of one person and search criteria of another. So you'd have something like this:
In this example, the first person would be you... the one logged in to the site. The second person would be no one at first.. it's just person(). But the next line would call what is called a Method called find... and find anyone in the database that is a female. To see this a little better, here is our new class:
Obviously this will only work if one person in the database is female, since I have it returning a row instead of an array, but bear with me... this is just an example. Anyway... you can see how the personA was created right in the new person($_SESSION['id']) call. The second person called the find() method instead since there was no id to use. When you are referencing a variable right inside the object/class, you use $this-> since you could be talking about personA or personB... it just knows you mean this one. Now, outside of the object, you can now compare their likes and dislikes because you've created 2 'persons' using the same object.
In this case, they would not be a good match... their likes aren't the same.
So to summarize, you can create a single object with properties and methods.... and just make references to that object over and over and over again. Take cars for example, you can have one object/class called car... and create a truck, van, car, hybrid.... anything you want, just using different properties and methods within the car object. Then you can make a loop (or pull data from a database) that creates enough car objects to fill a car lot and have each one of them be completely different without ever having to write any car code more than once! The finishing code from the example in this tutoral is below, try it out for yourself!
|




