Object Oriented Programming provides raw power to PHP 4.1.xPart 1:Introduction
First off, an object (also know as a class) is a very simple section of code that has a section of its own variables and functions. In a simple way an object is kind of like a program itself.
Objects can be used for many different things as they are very expandable. What an object is capable of doing is entirely up to the developer. A class can be used for things as simple as creating a link and or to store data loaded from a file and or SQL query.
Part 2: Basic Syntax
The basic syntax of an object is quite simple. As you can see in the example bellow the syntax is much different from that of a function.
Class className
{
<? code ?>
}
|
Part 3: Object Variables
An Object can have variables declared inside the object. While it is not necessary for an object to have any variables it is most likely that they will. Most, if not all, objects use variables to store information that can be accessed at any time by any function within ,and outside of, the object. To create an object variable you must use the 'var' command when creating the variable. An example of variable declaration is listed bellow.
Class className
{
var $variable1;
var $variable2;
<? code ?>
}
|
The method of accessing object variables is different depending on if you are accessing the variables from within or outside of the object.
- Accessing From Within The Object:
To access a variable from within an objects own function you must use the '$this' reference. An example of this would be:
$localvar = $this->variable1; |
- Accessing From Outside of The Object:
Accessing a variable from out side of an object isn't that dissimilar from accessing one within the object. Instead of using the '$this' reference you use the name of the object you wish to access. An example of this would be:
$localvar = $object->variable1; |
Creating an object function is not that dissimilar from creating a normal function. To create an object function all you have to do is create a function inside of the objects brackets as shown bellow.
Class className
{
var $variable1;
var $variable2;
function classFunction($arg1, $arg2)
{
<? function code ?>
}
}
|
Part 6: Object Constructors
Although not required it is good practice for each object to have a constructor. A constructor is a function within the object that is called when the object is created. A constructor is mostly used for setting default and or initiating values for the objects variables.
Class className
{
var $variable1;
var $variable2;
function className($arg1, $arg2="default value")
{
$this->variable1 = $arg1;
$this->variable2 = $arg2;
}
function classFunction($arg1, $arg2)
{
<? function code ?>
}
}
|
Part 7: Using The Object
Using an object is quite simple. First We must create the object. This is done by setting a variable using the new command as shown bellow:
$object_var = new className("test");
|
Using an objects functions is also quite simple. To do this we will once again be using the '->' (reference/arrow) operator as shown bellow:
$object_var->classFunction("value1", "value2");
|
$return_val =
$object_var->classFunction("value1", "value2");
|
Remember, objects to take up more memory then standard functions and variables so if you can accomplish the same task without using an object I recommend you do so.

10 Random PHP Tutorials :
10 Random LearnPHP.org Tutorials:





