Tizag.com Webmaster Tutorials - A collection of webmaster tutorials from HTML to PHP.

Sunday, September 14, 2008

PHP Classes

What are classes?

Classes are object-oriented programming for PHP.

What is object-oriented programming?

Object-oriented programming consists of three main vocabulary words: classes, methods, and objects. An object is basically a data structure (also known as an abstract data type), which are encapsulated in a set of routines known as methods. A class is a collection of methods and objects.
What's the purpose of classes in PHP?

It's the same reason as any other programming language: for large projects, classes provide superior organization and less repetitive code.


A Simple Example:
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
?>


As you might have guessed, the line "class dummy {" initializes the class. "dummy" is now inherited as the name of the class. The purpose of this will be shown later. Declaring global variables in classes however are a tad different then regular PHP. They must be preceded by "var". (For all you C coders, this is identical to "int variable;") Next, we see a function in it's most purest form. "function sum($one, $two) {" intializes the function by the name of "sum" with the required arguments $one and $two. The function now adds the value of $one and $two, and returns the value quite obviously through "return $val;". Got it? Time to add more :-)


An Advance Example of Class

<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
function sum2() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
?>

What we just did right there is one of the most important aspects of object-oriented programming called "inheritance." Let's look at this line for line. All our variables are declared through the "var" procedure, that's nothing new. Nor is the first function sum(). However, sum2() is. First off, you might notice there are no arguments for this variable. That's because this variable is a void, much like numerous internal PHP functions like time() or others I can't think of. This line displays the inheritance. The "$this" variable is a reserved variable in object-oriented PHP programming (translation - don't use it in your classes!) that actually does the inheritance. "$this->sum()" is basically interpreted as, "in this class, perform the sum() function." Like "$this->sum()", "$this->variable2" is interpreted as "in this class, use the value of 'variable2'." Likewise with $this->variable3. Note: all calls to native class functions or variables are done in this manner. Starting with "$this->" and followed by the name of the function or variable you wish to use. I can't stress the importance of that enough. Now I want to show you something imperative to object-oriented programming, using all this outside of your classes. This is how it's done if your class is contained in an external file:


<?
include("/path/to/the/file/with/your/class/in/it");
$yourclass = new yourclassname;
echo $yourclass->somevariable;
?>

If the class is internal, simple exclude the include() line. Now this is more inheritance. "$yourclass" inherits all of the class because of the usage of the "new yourclassname". Obviously, you would replace "yourclassname" with the name of your class in your script. "$yourclass" is now used the same way as "$this" was inside of your class, as shown from the "$yourclass->somevariable" reference. Got all that? Good.
As if you haven't been bogged down with inheritance enough yet, I've got some more for you.


<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
function sum2() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
class dummy2 extends dummy {
function sum2_clone() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}

?>


Ok now this is just fun. The "sum2_clone()" function will return the exact same result (3) as will the original "sum2()" function! If you didn't guess yet, the "extends" does this. "extends" inherits all the values of one class for use in a completely different class. IMPORTANT - the name that follows "extends" is *always* the inherited class. In our example, the "dummy" class is inherited. Therefore, we can access "$this->sum()" and everything else from the "dummy" class. How cool is that?
I've got more about "extends" for you (and a new vocabulary word); constructors.


<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
function sum2() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
class dummy3 extends dummy {
function dummy3($variable1, $variable2) {
$val = $this->sum($variable1, $variable2);
return $val;
}


?>

So what's so special about that? What makes "dummy3" special is how you call it. You'd do it like this:


<?
include("/path/to/file");
$1 = 1;
$2 = 2;
$sum = new dummy3($1, $2);



?>

See how you can pass arguments right inside of your "new dummy3()" call? That's what's special about this. Though it might seem rather pointless, this is great to use for functions you use repetitively and want to make calls to with fewer lines of code. "$sum" will now return 3 if you did "echo $sum" by the way.
The next part of this tutorial is the "::" operator. To be honest, the example shown in the PHP manual is much better then anything I could write. However, I'll give it a whirl.


<?
class dummy4 {
function test() {
echo "I am from class dummy4.";
}
}
class dummy5 extends dummy4 {
function test() {
echo "I am from class dummy5.";
dummy4::test();
}
}

?>

Now, if you were to call the "test()" function from "dummy5" you would see this:
I am from class dummy5.I am from class dummy5.

I think this is pretty self explanatory. The :: operate is simply another way to make a call to something from another class just as "$this" did.
Now if I haven't held your hand through this tutorial, it's time for some parenting. Watch the example and learn.

class dummy6 {
function test() {
echo "I am from class dummy 6.";
}
}
class dummy7 extends dummy6 {
function test() {
echo "I am from class dummy 7.";
parent::test();
}
}
?>


This will return:

I am from class dummy 6.I am from class dummy 7.

"Parent" basically replaces "dummy6". The value of "parent" will always be the name of the class that follows "extends". Got all that? Good.
Finally, the last part of this tutorial deals with "references inside the constructor." Don't worry, it's easier then it sounds. Think of it as adding more to our constructor then just one function. Let's go.

class dummy8 {
function dummy8($text) {
echo $text;
}
}
function clone() {
echo $this->dummy8($this->text);
}
}
?>


Then you would do this:

include("/path/to/file");
$text = "Hey there.";
$class = new dummy8($text);
echo $class->clone();
?>


Can you see what's going to happen? "$class->clone()" will print:

Hey there.

See how it works? "$text" is now recognized as a variable inside the class, so "$this->text" is a proper reference to it. Get it? Good. And that's everything, I hope your mind isn't too fried :P Any questions/comments/complaints/suggestions/hate mail can be sent to Ben Wirth. Happy PHP-ing!

No comments: