January 11, 2012 learn_php

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’.


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:

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:

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:

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.

Next: Part 2, Constructors and Destructors.

One thought on “Object Oriented PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.