Object Oriented Programming- The beginning / Page 2 | WebReference

Object Oriented Programming- The beginning / Page 2


[prev]

Object Oriented Programming [con't]

In the first part of this article, we looked at what Object Oriented Programming involves, as well as what an object is. We also discussed classes and the steps to construct them. In this article, we will start to build our Banking application using the knowledge that we gained previously. At the same time, we will also discuss some of the more advanced features of Object Oriented Programming.

The Bank


Our application is going to try to simulate a bank account. We know that a bank account has the following features:

  • Account number
  • Account type
  • Account customer name
  • Date created
  • Current balance

We also know that the following actions can be taken on a bank account:

  • It can be closed
  • It can be opened
  • Withdrawal can be made
  • Deposit can be made

There are other features and actions that can be performed on a back account but we are not going to look at bank accounts in their entirety only the basics, this way we avoid over complicating the exercise. The purpose of this whole exercise is to show the usefulness of object oriented programming as opposed to really wanting to create a banking system. Translating the above points into software is easy when you think of a bank account as an object:

Figure 1:Class layout

Just by looking at the above picture, we can work out what methods we need our class to have:

Methods

  • We need to be able to generate an account number
  • Account types: Savings or Current Account
  • Maintain/update Balance
  • Open/Close Account
  • Withdraw/Deposit
The next thing we need to look at is where to store the information about the

account. Obviously, the best place to store information relating to bank accounts is in a database. To work with a database (from an OOP point of view) will require the following methods:

  • Connecting to the database
  • Inserting account details
  • Updating the balance on any withdrawal or deposits made

Our class will then be called Accounts and we will have a constructor method with the same name that will help us initialize some variables. Below is a screenshot of what our Bank looks like:

Figure 2

The Database

The database, called ‘bank,' will have two tables, one called accounts and the other called customer. Each will hold information about either the account or the customer. The two tables will be linked through a foreign key. The customer table has the following fields:

Customer Table:

Field

Description

cusid

Creates a unique customer id for each new customer

name

Stores the customer name

address

Stores the customer address

acc_id

Links the customer to a account in the accounts table

Accounts Table

Field

Description

accid

Creates a unique account number for each new account

accno

Stores the account number

type

Stores the account type

balance

Stores the account balance

active

Shows the account status

Since one customer can have many accounts, I thought it only right to insert a foreign key acc_id into the customer table. In addition, instead of having fields such as date created and date closed, I simply use the active field to check if the account is active or not. This will enable use to focus more on the programming than on particulars of the database. Below is the SQL for the database. Simply copy and paste the information into a MySQL client and run the code. You should have two empty tables:

Inheritance

Before we start to build our class, we need to look at a few more OOP concepts to have a better understanding of what we will be doing when constructing our class. Inheritance occurs when one class (B) inherits the capabilities of an already existing class(A). So the new class is created by extending the existing class with the extend keyword. To demonstrate these concepts, add the following code to a page:

Now add the following code to a script and run it:


Result:

Your monthly payment is: $833.33
The total property defined in class A is set to: $250000
Total sum is: 600 

Now let's look at the code. First, when you look at the class definitions, you will note that class B has been extended to use class A's properties:


Class A has one public member variable called total, that is set to 250000 dollars:
public $total = 250000;
Then it defines a method that calculates the monthly payments that are to be made:


Class B has one method that simply adds up two digits:


Class B then extends class A, meaning that B will now be able to access all the methods and (public) properties of A, in addition to the methods and properties that are already available to itself. In the code that is located in the main PHP script, we can see just how much access class B has. First, we go through the process of instantiating class B to create an object (in this case, the object is called obj) using the new keyword:


Then we access and define a method called monthly() that is actually declared in class A and then we display the results:


Because the public member variable is visible to all, class B can access it and display its contents:

echo "The total property defined in class A is set to: $".$obj->total."
";

Finally, we access the methods declared in class B itself:


Another concept that follows shortly on the heels of inheritance is overriding. For example, in class A we have a public member variable that has a value of 250000. If we want to alter that value to reflect something else then we can create class B to look something like this:


So, what have we done here? We simply copied the entire class A structure, but we changed the value of the public member variable from 250000 to 300. Therefore, class A and class B will now look something like this:


The code in the main PHP script is set to:


The only thing that has changed here is that we removed the call to the add() method of class B. When you run this code, you get the following results: Your monthly payment is: $1
The total property defined in class A is set to: $300 What you see here is called overriding. Though the code and values of class A (which in this case is the parent class) remains unchanged, the code in class B that has inherited the methods and attributes of class A has overridden the value of the public member variable.

In the next article, we will look at some more of the concepts involving constructors.

Original: October 12, 2009


[prev]