[Tutorial] PHP Classes

[Tutorial] PHP Classes

Postby Roswell » Sun Dec 30, 2007 7:06 pm

With the introduction of PHP4, PHP introduced classes, a type of programming subdivision typically only seen in desktop languages. PHP5 expanded upon this implementation, creating what many consider to be a nearly full object orientation implementation. This tutorial will focus on only the basic realm of classes, which is compatible with both PHP4 and PHP5, unless otherwise noted. Note that you need a decent understanding of PHP to read this :D (including the understanding of how functions work)

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
This is called a class instantiator. It loads an instance of a class. As you can tell, within (or outside) of the class, whenever I want to call a function within the class, I can't refer to it directly. Instead, I have to refer to it with the instantiator. Therefore, it looks something like
Code: Select all
$instantiator->class_function(arguments); 
as opposed to just
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
we'd have
Code: Select all
function __construct($height$color$width$return
You'll also notice my fourth argument ($return) works a bit strangely. Depending on a few things, PHP will return the class output if you're using a default function to $this as well (note that it doesn't have to be named $this, I just choose to since it's more obvious). I use this to make sure $cat isn't returned to $this when I call $this -- if that happened, all of my $this references wouldn't work, as an array would be returned, so $this would no longer be an object (which would throw an error regarding calling a member function of a non-object).

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(1442"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 :D
Roswell
Mod Team
Mod Team
 
Posts: 2607
Joined: Thu Jul 05, 2007 5:06 pm


Re: [Tutorial] PHP Classes

Postby super3boy » Mon Dec 31, 2007 7:09 pm

Great Tutorial! 10/10
Image
User avatar
super3boy
Site Admin
 
Posts: 4312
Joined: Sun May 20, 2007 3:57 pm



Return to PHP

Who is online

Users browsing this forum: Ask Jeeves [Bot] and 1 guest