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 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 ?>} |
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 ?>} |
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; |
- 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; |
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 ?> }} |
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 ?> }} |
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"); |
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"); |
$return_val = $object_var->classFunction("value1", "value2"); |
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.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 fo...
- Working with Objects in PHP 5The 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 ClassThis 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 OrientedOne 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 ClassYou 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 JavaThat 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 VariablesNow 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 ProgrammingWell 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 ShadeThis 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.
