Legal Stuff
You are free to distribute and reproduce this tutorial as long as you include this section (unaltered) and give credit to me. This tutorial was written by Michael Roddewig.
Introduction
Most people are afraid of classes and refuse to see the use of them. I was, until I took a JAVA class, which requires the use of classes, and saw the actual beauty of them. Classes are really useful, they reduce the clutter of your code, and make updating a site very easy. Unfortunately, classes aren’t as useful in PHP as in other languages, but I still like them. I’m going to use my site as an example, where I have the user_commands class, which handles all the users on the site (ie login, logout, session_ids, etc). It makes my code very neat and organized, and it really pays off when you need to make a change.
Here we go!
I won’t go into a lot of terminology, like most people do. I’ll just tell you what you need to know for PHP. I’ll admit, I don’t even understand everything about classes. Most of my knowledge comes from JAVA, PHP’s support of classes is very simple. (Mostly because it is a scripting language)
Let’s say you want a page to display pictures. With classes, each picture would be defined as an object, which literally is a variable (as you will see it in your code). This variable (object) can contain functions and other variables (such as the height and width of your picture). You can create more objects (if you have more pictures) and delete them. It really simplifies making a picture, because you don’t have to write all the html out, and it makes your code nice and neat.
Now, some people may ask, why not just make a function? Because this is a classes tutorial, shut up and let me speak! :D You’ll see the beauty of classes later.
Now down to the code…
Okay, a class is sort of like a super function, so you declare almost like one.
class picture {
// Your code here
}
You basically have 2 different things you can put in a class: a function or a variable.
class picture {
function make_picture {
// Function code here
}
var $picture_size;
}
Now, we have to find out how to access this class, and use our nifty create_picture() function. I’ll do this from a different file. Let’s say that the class is stored in a file called ‘picture.php’.
require_once('picture.php');
$nicepicture = &New picture;
OMG, stop right there! What the heck is that? Well, remember I was telling you about how a class is a varible? Look, I just made a variable called $nicepicture. $nicepicture has the create_picture() function in it and the variable $picture_size. The &New tells PHP you’re making a new object and picture is the name of the class you want to use. Now, let’s go a little further and see how to use our function.
require_once('picture.php');
$nicepicture = &New picture; //We create our variable
// Now let's use the create_picture() function
$nicepicture->create_picture();
Now, let’s analyze that command. First, we used the $nicepicture variable. The ‘>’ means we’re referring to something inside it (this can only be used with classes, I’ve simplified things quite a bit). Then we call the create_picture() function. This will run whatever code we have in there. If you want to send variables to the create_picture() funciton all you have to do is ’$nicepicture>create_picture($somevariable);’. That’s all there is to functions!
Now let’s look at variables. We have that $picture_size variable on there. This is accessed the same way. Take a look at the code…
require_once('picture.php');
$nicepicture = &New picture; //We create our variable (called an object)
$nicepicture->picture_size = 100; //or whatever you want to put in it
Isn’t it simple? Classes aren’t so hard. Now I have to mention one thing I didn’t include earlier. When you’re writing a function for our picture class and you want to use the picture_size variable you have to call it as if you had already made the class, but you have to use a special variable (reserved, don’t use this as an object name). See the code below.
class picture {
function make_picture {
// Let's pretend we want to output the picture size, as an example
echo $this->picture_size; //We had to use the $this variable, which literally refers to THIS class. Simple isn't it?
}
var $picture_size;
}
Conclusion
Well, that concludes this simple tutorial. I hope you understand classes better than you did before. Now, go off and read the PHP manual on OOP. It goes into a lot more depth than I did. Thanks for reading!
