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 ofcode that has a section of its own variables and functions. In a simpleway an object is kind of like a program itself.
Objects can be used for many different things as they are veryexpandable. What an object is capable of doing is entirely up to thedeveloper. A class can be used for things as simple as creating a linkand or to store data loaded from a file and or SQL query.

Part 2: Basic Syntax
The basic syntax of an object is quitesimple. As you can see in the example bellow the syntax is muchdifferent 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 variablesdeclared inside the object. While it is not necessary for an object tohave any variables it is most likely that they will. Most, if not all,objects use variables to store information that can be accessed at anytime by any function within ,and outside of, the object. To create anobject variable you must use the 'var' command when creating thevariable. An example of variable declaration is listed bellow.

Class className{var $variable1; var $variable2; <? code ?>}

Part 4: Accessing Object Variables
The method of accessingobject variables is different depending on if you are accessing thevariables 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 weused the '->' (reference/arrow) operator. This is because we arepointing to the variable within the object.

- Accessing From Outside of The Object:
Accessing a variable from out side of an object isn't that dissimilarfrom accessing one within the object. Instead of using the '$this'reference you use the name of the object you wish to access. An exampleof this would be:

	$localvar = $object->variable1;

Part 5: Object Functions
Creating an object function is not that dissimilar from creating anormal function. To create an object function all you have to do iscreate 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. Thisfunction can not be called from out side of object unless you use apointer reference. We will talk more about that later on. Also, afunction can have any amount of arguments that will. I have used twoarguments above called $arg1 and $arg2.

Part 6: Object Constructors
Although not required it is good practice for each object to have aconstructor. A constructor is a function within the object that iscalled when the object is created. A constructor is mostly used forsetting 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 adefault value. This is what the value of $arg2 will be if the user doesnot 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 variableusing the new command as shown bellow:

$object_var = new className("test");

Notice how I passed a value when creating the object. This is donebecause it is required by the object's constructor function. If both ofthe object's constructor function's arguments have a default value thenthis would not be required.
Using an objects functions is also quite simple. To do this wewill once again be using the '->' (reference/arrow) operator asshown 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 developerbut try not to over use them. From my experience objects are best usedfor 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 andvariables so if you can accomplish the same task without using anobject I recommend you do so.

Related Articles

  • Object Oriented Programming With PHP 4.1.x
    Object 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 fo...
  • Working with Objects in PHP 5
    The second part of the series showing new PHP programmers how to deal with Objects and learn the basics of Object Oriented Programming.
  • Object Orient Programming: Image Class
    This tutorial is a Web Programming in PHP article on Object Orient Programming: Image Class. The author says, I share the code I use for my image class. Four functions: Upload, Resize, Get a file extenstion, Resize on the fly.
  • Intro To Object Oriented
    One thing you dont see too much, is tutorials on object oriented php. Now some may be familiar with object from Java and C++, and then on the other hand, many of you may have no clue what object is. For this tutorial, you need to be pretty fluent in php.
  • Intro To Object: Creating Your First Class
    You have havent done so yet, I strongly recommend you first read my Intro To Object before starting this tutorial.
  • Platform Independent Client-Side Applications with Java
    That very umph comes with Java. Java was originally developed at Sun Microsystems in 1991 to provide a platform-independent programming language and operating system for consumer electronics (TV sets, toasters and VCRs)...
  • Is It Wrong To Have A Goal-Oriented Home Page?
    Not too long ago, a client asked me to create a new design for his companys home page. As I was getting ready to work on the design, I asked my client what the homepage needed to accomplish. In other words, what were the pages primary goals? What did the company want clients to do when they got .....
  • Intro To Object: Option Variables
    Now that we have our first simple class (which we build in Intro To Object: Building Your First Class) were going to modify it a bit.
  • Introduction to Procedural Programming
    Well start of with if
    Here is the basic usage of it
    variable would be something like $variable
  • Basic Design Principles Part 2 Tone, Texture, Light, And Shade
    This specifically applies to drawings more than photography, but tone and texture are very important. Tone refers to shading of light and dark on an object and texture is the visual and tactile surface characteristics of an object.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.