Object Oriented Programming
This tutorial will teach you OOP (Object Oriented Programming) in PHP. You should have basic PHP knowledge before reading this tutorial. First I will teach you how to make a class, and use it within PHP.
<?php
//
// Build the class, replace ClassName, with the name of your class
//
class ClassName {
//
// Set a variable, will be set to a value later
//
var $variable;
//
// Create a function to print out the variable
//
function myfunction() {
print($this->variable);
}
//
// End the class
//
}
?>
This will create a class, but will not really do anything until you build it later. Now, for a little explanation, the variables set in classes need not be set to a value, because later in the script you can set it from outside the class block. You may have of noticed the $this->variable statement. When using classes, you use relation to that class, and variables you want to use while you have code in the class, you can use the $this->{VARIABLENAME}. You may have of also noticed that there is no $ in front of variable, classes don't work like that. Now I can show you how to build a class from outside, and print out the variable.
<?php
//
// Build the class, replace ClassName, with the name of your class
//
class ClassName {
//
// Set a variable, will be set to a value later
//
var $variable;
//
// Create a function to print out the variable
//
function myfunction() {
print($this->variable);
}
//
// End the class
//
}
//
// Create the object
//
$build=new ClassName;
//
// Set the variable within the object (notice no $ in front of variable)
//
$build->variable="Hello, this is some text.";
//
// Call the function from within the object
//
$build->myfunction();
?>
This will print out: Hello, this is some text.
The $build variable (after creating the object through it) now relates everything to it with the class, so you could also do it like this:
<?php
//
// Build the class, replace ClassName, with the name of your class
//
class ClassName {
//
// Set a variable, will be set to a value later
//
var $variable;
//
// Create a function to print out the variable
//
function myfunction() {
print($this->variable);
}
//
// End the class
//
}
//
// Create the object
//
$build=new ClassName;
//
// Set the variable within the object (notice no $ in front of variable)
//
$build->variable="Hello, this is some text.";
//
// Call the variable from within the object
//
echo $build->variable;
?>
Will print out the same thing as the previous example. For a better understanding of the functions inside of a class, here is an example of some math:
<?php
//
// Build the class, replace ClassName, with the name of your class
//
class ClassName {
//
// Set a variable, will be set to a value later
//
var $variable;
//
// Create a function to print out the variable
//
function add($varA,$varB) {
$this->variable=$varA+$varB;
print($this->variable);
}
//
// End the class
//
}
//
// Build the object
//
$build=new ClassName;
//
// Call to the function within the object
//
$build->add(5,5);
?>
As you probably may have guessed, this will print out 10. Just like a normal function in PHP, except called by the object using the $build variable, then stores the answer in the $variable variable. This means you can do this:
<?php
//
// Build the class, replace ClassName, with the name of your class
//
class ClassName {
//
// Set a variable, will be set to a value later
//
var $variable;
//
// Create a function to print out the variable
//
function add($varA,$varB) {
$this->variable=$varA+$varB;
print($this->variable);
}
//
// End the class
//
}
//
// Build the object
//
$build=new ClassName;
//
// Call to the function within the object
//
$build->add(5,5);
//
// Call the variable, which now has the value of 5+5 in it
//
echo "".$build->variable;
?>
This will print out:
10
10
Now, you can use the extends method in OOP, so one class extends into another class, obtaining all of it's functions, variables, etc..
<?php
//
// Build the class, replace ClassName, with the name of your class
//
class ClassName {
//
// Set a variable
//
var $variable;
//
// Create a function to do something
//
function dosomething() {
//
// Reset the variable to a new value (change e's to 1's)
//
$this->variable=ereg_replace("e","3",$this->variable);
//
// Print out the variable
//
print($this->variable);
}
//
// End class
//
}
//
// Build the class, you can rename it, nothing has to be put in here
//
class DiffClassName extends ClassName {
//
// End the class
//
}
//
// Build the DiffClassName object
//
$build=new DiffClassName;
//
// Set the variable to something
//
$build->variable="There are a lot of e's in this sentence eh?";
//
// Run the function from the object
//
$build->dosomething();
?>
That should print out:
Th3r3 ar3 a lot of 3's in this s3nt3nc3 3h?
As you can see, the function in the first class, will replace the e's with 3's. You may notice that we built the DiffClassName object, instead of the ClassName object to output the sentence. This shows us that you can use the extends method to output data obtained from the parent class. This is all for my tutorial.