PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Object Oriented Programming With PHP 4.1.x
Your Ad Here

Object Oriented Programming With PHP 4.1.x


Object Oriented Programming With PHP 4.1.xObject Oriented Programming provides raw power to PHP 4.1.x

Part 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 ?>
}

In the above code I have created a simple object that is named "className".

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 ?>
}

Part 4: Accessing Object Variables
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;

Notice how we didn't not use the '.' (period) operator but instead we used the '->' (reference/arrow) operator. This is because we are pointing to the variable within the object.

- 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;

Part 5: Object Functions
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 ?>
   }
}

In the above code I have create a function called classFunction. This function can not be called from out side of object unless you use a pointer reference. We will talk more about that later on. Also, a function can have any amount of arguments that will. I have used two arguments above called $arg1 and $arg2.

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 ?>
   }
}

As you can see above I have given $arg2 in the constructor function a default value. This is what the value of $arg2 will be if the user does not pass any value to $arg2.

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");

Notice how I passed a value when creating the object. This is done because it is required by the object's constructor function. If both of the object's constructor function's arguments have a default value then this would not be required.
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");

If the function returns a variable then you would use the function as follows:

$return_val = 
     $object_var->classFunction("value1", "value2");

Part 8: ConclusionObjects can be a great tool for any developer but try not to over use them. From my experience objects are best used for storing multiple pieces of information and or for easy to use, automated, tasks such as creating html/xml style tags.
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.

Author's URL: David Serrano
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Object Oriented Programming With PHP 4.1.x"

Only registered users can write comment

No comments yet...