So what exactly is a class?
In the most basic sense, a class is a group of related functions. Think of them as parts of a factory: even though they do different things, they usually work together to create a common result.
So what does a class look like?
If you pop open the source of your favorite PHP script, you'll probably see something like this (we'll call it lolclass.php in this tutorial.)
- Code: Select all
<?php
$this =& new lolCat('', '', '', false);
class lolCat
{
function lolCat($height, $color, $width, $return = true)
{
if($return)
{
$cat = $this->makeLolCat($height, $width);
$cat = $this->colourLolCat($cat, $color);
return $cat;
}
}
function makeLolCat($height, $weight)
{
$i = 0;
$j = 0;
while($i < $height)
{
$lol[$i]['message'] = "Hi, I'm a lolcat!";
while($j < $weight)
{
$lol[$i]['weight'][$j] = "I'm " . $j . " times fatter now.";
$j++;
}
$i++;
}
return $lol;
}
function colorLolCat($arr, $colour)
{
foreach($lol as $key => $value)
{
if($value == "message")
{
$lol[$key][$value] = "<font color=\"" . $colour . "\">" . $lol[$key][$value] . "</font>";
}
else
{
foreach($lol[$key][$value] as $new_key => $new_value)
{
$lol[$key][$value][$new_key] = "<font color=\"" . $colour . "\">" . $new_value . "</font>";
}
}
}
return $lol;
}
}
?>
Suprisingly, this code should look rather familiar to you. It's essentially a group of functions, nothing more. I'd only like to direct you to three things:
- Code: Select all
$this =& new lolCat;
- Code: Select all
$instantiator->class_function(arguments);
- Code: Select all
normal_function(arguments);
The second thing I'd like to point you to is our first function, named lolCat (the same as the class name). When a class is called, PHP checks to see if there is a function named after the class. If so, any arguments for the class are passed to that function. In PHP5, you can also name your function __construct (two underscores). Therefore, instead of
- Code: Select all
function lolCat($height, $color, $width, $return)
- Code: Select all
function __construct($height, $color, $width, $return)
So, how do I implement this?
That's where our file myscript.php comes in. It's actually quite simple.
- Code: Select all
<?php
include('./lolclass.php');
$lulz =& new lolCat(14, 42, "blue");
print_r($lulz);
?>
We include our file with our class, call it (note that the =& sign can also be = -- when calling a function or a class [including ones provided by PHP itself, like fopen() or something like that], you can use an & sign to supress any errors it throws upon instantiation. I do it out of habit, as it's usually the desired outcome), then print_r it (only since it returns an array and it's an easy way of showing the data that's in it [normally you actually use the results the class outputs]).
So what are these good for?
As I said earlier, classes are good for grouping similar functions. Note that they hardly ever actually output anything -- instead, they provide information for a script to manipulate.
Good luck, and happy PHPing

