11
2012
Object Oriented PHP
Object Oriented PHP – Page 1
Before we combine PHP with Object Oriented Programming, we first need to understand the basics of OOP. OOP is a paradigm that has been around for quite a few years now, and it utilizes classes, relations and properties. Software that is based on OOP, can be sesn as a self contained object that include attributes and operations. Attributes are represented as properties, or better yet, the variables of that class. The objects can be referred to as a set of methods as one would call them in java, and in PHP, we just call them plain functions. Simply put, operations are the processes, or actions that will take place when a class is being ‘summoned’.
Advantages
One of the greatest advantages of OOP is data hiding. This means that only authorized processes are allowed to access data that exists within a certain class. The correct term for this would be encapsulation. This data is accessed via an interface specifically made for these processes.
Another advantage in using OOP is the ability to maintain the software much more efectively without having to alter the interface. This maintenance goes hand and hand with the ability of encapsulation. All in all, Object Oriented programming can be of much help when creating large scale, complex projects, increase your ability to reuse code and at the same time, be cost effective.
Create a class in PHP
Creating a class in PHP is very simple. All you need to do is use the keyword class. The following is an example:
|
1 2 3 4 5 |
<?php class myClass { } ?> |
Remember that class naming conventions follow the same rules when creating a variabel, except the use of the dollar sign in the beginning.
In order for this previous class to have a purpose and be useful to us programmers, we need to add attributes and operations. To be able to create attributes, we need to use the keyword var. Let’s create attributes to our previous class:
|
1 2 3 4 5 6 7 |
<?php class myClass { var $product; var $quantity; } ?> |
The previous example creates the class with name myClass, and has 2 attributes. Notice how I use var in front of each attribute, and those attributes are in between the 2 curly braces of the class. Attributes must be within those curly braces in order to pertain to this class. The same rule applies for the operations, so let’s go ahead and add an operation to our current class. This time though, we would call our operations with the keyword function:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php class myClass { var $product; var $quantity; function add_quantity($quan1, $quan2) { } } ?> |
This add_quantity function, as you can see, takes 2 parameters. In order to use the attributes inside the class, we would need to use the keyword, this. If the keyword is missing, PHP will assume that you are trying to use a different variable that is not an attribute to the class. let’s add functionality to the function that we just created.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php class myClass { var $product; var $quantity; function add_quantity($quan1, $quan2) { // By the name inferred to it, we will make this // function add the 2 parameters passed on // and give that value to our attribute, quantity $this->quantity = $quan1 + $quan2; } } ?> |
Next: Part 2, Constructors and Destructors.
An article by





In PHP 5 the keyword var is replaced by private, protected and public for declaring variables / properties.
“If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public”
More info here: http://www.php.net/manual/en/language.oop5.properties.php
Thanks
Derek