Object Oriented Programming- The beginning | WebReference

Object Oriented Programming- The beginning

By J. Leidago Noabeb


In this article, we will be focusing on object oriented programming. At the end of the series, we will be creating a complete banking application to demonstrate the concepts that we have learned here.

What is object oriented programming?

It is essentially a different way of constructing your applications. Instead of having loose procedures and functions that are meant to serve one purpose, Object Oriented Programming enables you to put data and functions in one container. This container is referred to as an object. An object enables you to model your application as closely as possible to the real world. For example, when a house is being built each worker concerns himself with his or her own work, i.e., a plumber will only concern himself with plumbing while the electrician will concern himself with the electrical systems. The plumber does not need to know where the electric wires will go in a bedroom. Neither does the electrician have to know where the water pipes are located. The person with overall responsibility for the construction of the house only has to concern himself with the time it takes to complete the house and not with the particulars of a task. Object Orientation works in a similar fashion; an object hides its implementations from others, because its functions are often irrelevant to the functions of others. One of the advantages of coding with OOP is that it enables you to model real world objects to your applications.

For example, we know that a bank account has the following attributes:

Account number, Account holder, account type, balance, etc., so it is easy then to build an object that has these attributes. Another benefit that OOP offers is, of course, code reuse. Often while coding, I find that I repeat the same code repeatedly at different places in an application. This can be simplified by simply creating an object that holds all the data centrally.

The Object

An object is a fancy variable that contains methods and attributes and data that work together on that data. Objects are usually grouped into classes. A class contains objects that all have the same operations or methods behaving in the same way and attributes or properties representing the same things. A PC can be thought of as a class of objects with many common attributes such as a screen, a keyboard or mouse and methods or operations such as displaying text on screen. To create an object you need a class. A class is the framework for an object that defines the functionality and variables contained within. So how do we create a class? Take a look at the code below:

It is that simple, all you need to do is to use a keyword called ‘class' and than provide a name. It is almost as easy as defining a function. To use this class in your PHP script you do like this:

That's basically how easy it is to create and use a class. It's not really that exciting is it? To make it more useful we need to add what is called a method. A method is just a function that is defined inside the class:

A method actually does something; this is why it makes a class useful. Below is a method that generates a seven digit account number, defined for our Account class:

To use this method in our PHP script, we do like this:

So what does the code do? First, we include the class.account.php script that contains the class that we require:

include "class.account.php";

Then we instantiate the account class. We effectively make a ‘copy' of the class and its methods, properties or attributes and store them in the $myclass object:

$myclass = new Account;

Now we use this object to access the newly created method:

Notice that to access the methods of an object, we need to use the -> operator. We can also add properties to our class. A property, also called an attribute, is declared as you would a variable. If you want to store a value, you assign that value to a variable in procedural PHP. In OOP, if you want to store the value of a property, you also use a variable. The variable itself is declared at the very top of the class. The name of the variable equals the name of the property. For example, if the variable is called custname then the property of the class is also called custname. Below is an example of our Accounts class with a property called custname:

To use this property in a PHP script we do like this:

When you run this script, you get:

Hi Dantago !Noabes

The basic purpose of the code is to show how to access and assign values to a class property from a normal PHP script. Notice again that to access the class property, we use the -> operator. Here we assigned the name David Web to the custname property of the class:

$obj->custname='Dantago !Noabes';

When we added the class property, we used the public keyword:

This keyword lets the class know that you want to be able to access the property from outside the class. Some member variables of the class should not actually be accessible from outside the class, but should rather be accessible to the class itself. For this sample code, I wanted to show how to access a class property from outside the class. But in most cases, a user won't know what variables are declared as class property, so it won't be possible for them to access, let alone use these variables.

The $this-> operator:

is used so that the object can get information about itself. This is because you might have multiple instances of a class, and you won't know in advance which object variable you will be using; the $this-> operator enables each object to talk to itself without having to know the name of the object variable currently referencing it.

A closer look at methods

There are three levels of visibility that a method or member variable can have. You have already dealt with the public key word and have learned its purpose. So let's take a look at the remaining two:

  • Protected – Protected members are available to the class itself and to those that inherit from it. We will be looking at inheritance later on.
  • Private - Private members are only accessible to the class itself. These are typically items used for internal housekeeping.
  • The default visibility level for any member is public unless explicitly stated otherwise. This means that you do not need to add the public keyword to any member variable that you declare. However, it is recommended that you always explicitly state it at all times.

The Constructor

When working with classes, there is a special method called a constructor that usually has the same name as the class itself. The main purpose of this function is to be automatically called when the class is instantiated. It is therefore the perfect place for initialization tasks such as setting values for properties and creating other objects needed by this object. It is declared in the same way as the other methods, but for its name, which is required to be the same as the class name.

The next section of this article will focus on the more advanced topics of OOP. We will also start to build our own class that we want to use in our banking application.