April 30th, 2008
PHP Object Oriented Programming
PHP Object Oriented Programming
There seems to be a common pitfall among some PHP developers–especially those just beginning PHP programming–and that is their lack of object-oriented (OO) PHP use. This article’s purpose is to inform developers about the practicality of Object Oriented Programming in PHP; fully understanding the benefits of using Object Oriented Programming in PHP should be a requirement in the PHP learning process.
Common Mistakes Done By Developers
I myself experienced the pitfall in the beginning of my PHP enlightenment, but the light shone again and my eyes opened to the use of Object Oriented Programming in PHP. What I mean by Object Oriented Programming in PHP specifically is the use of classes, not just simple function blocks. Many new developers feel that classes are useless, cumbersome, and time-consuming. Given the unique nature of how PHP actually works, it is possible to throw functions in the code at any point on the page and start using them immediately. If this is the case, it is possible to include a page of functions at the top of the page and go to town with them. Now, while it is possible to do this–and using classes isn’t very far from it in some aspects–there are numerous benefits and advantages to actually using classes instead of simply using a list of functions.
What are PHP Objects ?
To new developers, classes–or object oriented programming, for that matter–may seem a little intimidating or unfamiliar. For those of you who aren’t familiar with classes, here’s a brief introduction.
Note: I also want to tell you that PHP 5 has a new Object Model from PHP 4 but in this tutorial we have focused on PHP 4 because still it’s in demand.
Here’s a file called example class.php. Class definition looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php class Example { //class-wide variables var $var1; var $var2; //function to gather two numbers function set_numbers($number1, $number2) { $this->var1 = $number1; $this->var2 = $number2; } //function to add numbers together function add_numbers() { return ($this->var1 + $this->var2); } } ?> |
Note: Click here to download a zip file PHP Object Oriented Programming all the source codes which are used in this tutorial. Although it’s not necessary as however if you want you can download it for later reference.
Under the class-wide variables comment are two variable initializations: var $var1; and var $var2;. Variables defined in this manner are available throughout the entire class. You can access them in any function by using the syntax $this->variablename.
For example, the function set_numbers() assigns the $number1 argument to the classwide variable var1. The function add_numbers() returns the sum of var1 and var2.
To use this class, create an instance of the Example class object (exampleuse.php):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php /* Include the class file using require_once. It will require the class to be loaded (or the page won't load), and will only allow it to be included once, to avoid errors. You can also use require(), include(), or include_once() */ require_once("exampleclass.php"); //create an object variable for the instance //of the example object $exampleobject = new Example; |
This creates an object variable using the new function. The syntax is simple: $objectvariable = new ClassName;. As you can see, the name of the class in exampleclass.php is Example; this is the name you must use to assign the class to the object variable. With an instance of the Example class, you can use the functions (or methods) inside of it:
1 2 3 4 5 6 | $exampleobject->set_numbers(1,3); echo($exampleobject->add_numbers()); //the output will be: 4 ?> |
Using class methods is very similar to the $this-> operator, except that $this is replaced with the object variable. In this example, the object variable is $exampleobject. Both $exampleobject->set_numbers(1,3) and $exampleobject->add_numbers() call methods in this instance of the Example class.
Pages : 1 2 3 4

