PHP - HTML Preprocessor - Classes Tutorial
Mathieu 'CaPS' Kooiman
Version 0.1
====================================================
Introduction
1.1 What's this about?
1.2 What are classes?
1.3 The Author and Contributors
Classes
2.1 Basics
2.2 Constructors
2.3 Inheritance
2.4 A Little Tip
Introduction
1.1 What's this about?
======================
This is a sort of HOWTO describing how to use classes in the
scripting language PHP. If you don't know what PHP is
you're probably at the wrong spot, check out
http://www.php.net
1.2 What are classes?
======================
Classes in PHP are a form of "code clarification". In other
words, they can make your code make more sense to the human.
They provide ways to keep your data *and* functions separate.
I like to compare classes with little programs. You can look upon
classes asif they were little programs with global variables.
I still like Rasmus' definition of classes:)
-- "Classes in PHP is mostly Data/Function encapsulation"
1.3 About the Author and Contributors
==============================================
Well, I'm Mathieu 'CaPS' Kooiman, born in 1983 and live
in The Netherlands. Been coding in PHP for over a year now.
Other articles I've written:
* The PHP - Dynamic Loadable Module - HOWTO
Featured on http://www.linuxguruz.org
* Windows 98 SE - Internet Sharing
Featured on http://www.home-networking.org
Once again I'd like to thank Randall (AKA Ranman) for
proofreading/adjusting my article, which he also did
on the PHP-DLM HOWTO. Thx bud:)
The Beginning
2.1 Basics (Why use classes)
=========================================
Alright, let's start with the beginning. Say we have this
basket we want to fill with something. We could do this:
$Basket[] = "FRUIT";
$Basket[] = "COFFEE";
That's fine, nothing wrong with that. Now we'd want to empty it
too. We'd have to call a function like unset().
function DeleteItem($Basket, $Item) {
for ($i=0;$i { }
class EmptyBasket {
var $Content;
}
There! We got ourselfs a little (pretty damn useless) class now.
To use it we're going to have to create an INSTANCE of this class.
This is done using the 'new' keyword.
$Basket = new EmptyBasket;
Now you can assign something to the 'Content' variable which is
in the class EmptyBasket. To call a variable from a class you use
the '->' operator.
$Basket->Content[] = "FRUIT";
Alright, so far so good, we could have done this using the first
example right? Now lets add that function DeleteItem(). Functions
in Classes are usually referred to as Methods. Not in definition
though!
class EmptyBasket {
var $Content;
function DeleteItem($Item) {
while(list($var, $val) = each($this->Content))
if ($val == $Item)
unset($this->Content[$var]);
}
}
$Basket = new EmptyBasket;
$Basket->Content[] = "FRUIT";
$Basket->Content[] = "COFFEE";
$Basket->DeleteItem("COFFEE");
I can see you wonder..
"Where did $Basket go in DeleteItem() ?!"
"And what's that '$this' doing there?!"
Since we defined $Content in the classes main scope its
accessible throughout the whole class. All Methods can access it,
without defining it global or whatever. So we don't have to pass
variable to EmptyBasket->DeleteItem(). No mistakes like forgetting
to pass it by reference anymore!
To call a class' variable or function from a class' body we'll
have to use '$this'. This is because at that time there isn't an
'INSTANCE' which you can use to refer to anything.
Next we'll learn about Constructors.
1.2 Constructors
===========================
You can call a function upon creation, this function is called
a constructor. In C++ you have constructors and DEconstructors, yet
in PHP there isn't something like a DEconstructor yet you could use
Register_Shutdown_Function() for something LIKE a DEconstructor.
class EmptyBasket {
var $Content;
function EmptyBasket($Name) {
print "$Name requested a new EmptyBasket ";
$this->Content[] = "FREE_GIFT";
}
function DeleteItem($Item) {
while(list($var, $val) = each($this->Content))
if ($val == $Item)
unset($this->Content[$var]);
}
}
$Basket = new EmptyBasket("CaPS");
// Prints "CaPS requested a new EmptyBasket"
$Basket->Content[]="COFFEE";
while ( list ($Var, $Val) = each($Basket->Content))
print "Item #$Var $val ";
/* Prints:
* Item #0 FREE_GIFT
* Item #1 COFFEE
*
*/
2.3 Inheritance
========================
Another usefull feature when using classes is Inheritance.
This way you can have 2 separate classes, where one is a copy of the
first but with possible extensions. Per example: you could have
EmptyBasket with only EmptyBasket->DeleteItem(), and have a
LessEmptyBasket with ex. LessEmptyBasket->AddItem() which could use
EmptyBasket's DeleteItem() but _not_ Vice Versa.
Classes can inherit from another class using the 'Extends' keyword.
class EmptyBasket {
var $Content;
function DeleteItem($Item) {
# Method DeleteItem()
while(list($var, $val) = each($this->Content))
if ($val == $Item)
unset($this->Content[$var]);
}
}
class LessEmptyBasket extends EmptyBasket {
/* Class LessEmptyBasket inherits:
* * $Content
* * DeleteItem($Item)
*/
var $Owner;
function LessEmptyBasket($Owner) {
$this->Owner = $Owner;
print "$Owner requested a new LessEmptyBasket";
}
function AddItem($Item) {
$this->Content[] = $Item;
}
}
$Basket = new LessEmptyBasket("CaPS"); # Constructor LessEmptyBasket
$Basket->AddItem("COFFEE"); # Own function
$Basket->AddItem("FRUIT"); # Own function
$Basket->DelItem("COFFEE"); # Inherited function
Note: The new class constructor overrules the old constructor.
The old constructor now becomes a normal function which
you'll have to call manually.
Note: Functions in 2 classes which use inheritance can't
co-exist. So if you'd have Method TEST() in Class1
and Method TEST() in Class2 which extends Class1, and you
call the Method TEST(), TEST() from CLASS2 will be called
and TEST() from Class1 'll just be useless.
2.4 A little Tip
=============================
When you want to print a Object/Class' variable value you'd
propably go and do something like this:
echo "Hello there, " . $Basket->Owner . ", how are you?";
However there is a faster way to do this, with the (IMHO) ugly
concatentation operator. Try it this way:
echo "Hello there, {$Basket[$i]->Owner}, how are you?";
2.5 Goodbye *snif*
====================================
Well, there isn't much left for me to write except that I wish
you luck writing your own classes, and you can mail any
suggestions about this tutorial to 'Mathieu Kooiman '
- Mathieu