Object Oriented Programming PDF Print E-mail
User Rating: / 0
PoorBest 
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:

<?php
class person {
function person(){}
}
?>

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:

$person = new person();

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:

$personA = new person();
$personB = new person();

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.

<?php
class person {
var $gender;
var $hair;
var likes;
var dislikes;
function person(){}
}
?>

Now we can see that our person has properties that can be set, changed and read so that we can compare them.

$personA = new person();
$personA->gender = 'male';
$personA->hair = 'black';
$personA->likes = 'sports';
$personA->dislikes = 'opera';

$personB = new person();
$personB->gender = 'female';
$personB->hair = 'blonde';
$personB->likes = 'opera';
$personB->dislikes = 'sports';

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 $personA->gender directly links to the var $gender that is found within the person object. And this one variable can be set and read from each object separately. That's pretty powerful stuff.  In PHP, you can't have one variable be set to many things, and even if you use a function, it is still only set to one value, it just might be different values in and out of the function.

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:

$personA = new person($_SESSION['id']);
$personB = new person();
$personB->find('female');

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:

<?php
class person {
var gender;
var hair;
var likes;
var dislikes;

function person(id=null){
if( !empty($id) ){
$result = mysql_query('select * from users where id = ' . $id);
$row = mysql_fetch_row($result);
$this->gender = $row['gender'];
$this->hair = $row['hair'];
$this->likes = $row['likes'];
$this->dislikes = $row['dislikes'];
}
}

function find($gender){
$result = mysql_query('select * from users where gender = "' . $gender . '"');
$row = mysql_fetch_row($result);
$this->gender = $row['gender'];
$this->hair = $row['hair'];
$this->likes = $row['likes'];
$this->dislikes = $row['dislikes'];
}
}
?>

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.

$personA = new person($_SESSION['id']);
$personB = new person();
$personB->find('female');

if( $personA->likes == $personB->likes ){ echo 'We have a match!'; }
else { echo 'These two are not compatible.'; }

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!

<?php
class person {
var gender;
var hair;
var likes;
var dislikes;

function person(id=null){
if( !empty($id) ){
$result = mysql_query('select * from users where id = ' . $id);
$row = mysql_fetch_row($result);
$this->gender = $row['gender'];
$this->hair = $row['hair'];
$this->likes = $row['likes'];
$this->dislikes = $row['dislikes'];
}
}

function find($gender){
$result = mysql_query('select * from users where gender = "' . $gender . '"');
$row = mysql_fetch_row($result);
$this->gender = $row['gender'];
$this->hair = $row['hair'];
$this->likes = $row['likes'];
$this->dislikes = $row['dislikes'];
}
}


$personA = new person($_SESSION['id']);
$personB = new person();
$personB->find('female');

if( $personA->likes == $personB->likes ){ echo 'We have a match!'; }
else { echo 'These two are not compatible.'; }
?>

 

 

Add comment


Security code
Refresh

Login to TyCamTech.com

Donate

Must have Pepsi, feed my addiction here:


Recommended Books

Introduction to Programming
A Quick And Easy Ebook That Introduces Readers To Computer Programming In A Jargon Free, Easy To Understand Way.

Learn PHP in 17 hours!
If you can copy and paste into HTML and you can follow simple instructions...

Setting up a Web Server
Teaches All The Steps To Hosting A Website From Home.

Follow us on



Follow tycamtech on Twitter