Starting from PHP 5, the language introduced the classes. They derive from the Object-Oriented program languages, such as C++ or Java, and they are objects indeed.

I’ve set this tutorial as “PHP/Advanced“, but not because it’s difficult, but rather because you must know what a function is and have a little bit of familiarity with variables.

Objects in the programming jargon are “boxes” that include variables and methods to edit them. Usually the variables are “private“, which means that you won’t be able to see them outside the class, but they may also be public (for example logs), even if I discourage the use of them.

Methods are properly the “functions of the class”: they can read and write the class’ variables. A class is the “structure” of an object, and objects may be created and destroyed. For this, classes have a constructor and a destructor: if they’re not scripted, they will still exist in an undeclared state. But here is an example:

<?php

// definition a the class
class helloWorld {
	private $user;

	__construct($name) {
		$this->user = $name;
		echo "Hello $name!\n";
	};
	__destruct() {
		echo "Bye bye $this->user!\n";
	};

	function getUser() {
		return $this->user;
	}

	_destructor();
}

$hW = new helloWorld("Giacomo"); // call of __construct
echo "The user is: ".$hW->getUser()."\n";
unset($hW); // call of __destruct

?>

Hello Giacomo!
The user is: Giacomo
Bye bye Giacomo

Ok, let’s analyze this piece of crap :P

First of all, this is a basic construction of a class.
Costructor and destructor are special methods and MUST be called __costruct and __destruct (with two underscores). Constructor and destructor may also not be defined, if you think nothing should be done in those circumstances. They are in any case dealed as normal methods, so they follow the method’s rules.

Secondly a flash view about class’ variables: they MUST be declared in the main body of the class, they CAN’T be defined there (but only in the function-methods), and they are referred allways as $this->VARNAME (without $). Class variables are declared as “private” or “public“. You’ll not be able to access them directly but with a proper function (see getUser() in the example).

Then the methods come: they are obviously public by default, even if you may want to make them private for some reason. In this case you’ll have to add the word “private” before “function”, i.e. private function myPrivateMethod([arguments here]);
They usually return something (in case of “get” methods) or nothing (in case of “set” methods), it’s really up to you. I highly suggest not to print anything because if you return something you’ll allways be able to echo them, elsewise you’ll not even be able to “catch” the data, ’cause you’ll just print it. But of course, if it’s designed to just echo it, why not?

To create a new object of a class, just follow the syntax proposed in the code: $myVar = new className( [ eventual arguments here (see: constructor) ] );
To destroy an object, use unset($myClassVar); or just leave it as is: the destructor is called by default once the PHP script ends.

If you want to take some cue about classes, I may suggest you to see WoWASDK, a set of classes made by myself to browse the data included in the World of Warcraft Armory site. It’s a mix of classes, XML file opening (note that I had to use curl to download them and not directly using simpleXml because of a bug-feature of the site itself), string processing… pretty boring to write up, but you can see the power of a class ready to use!