How to Develop an Object-Oriented Script

Object-oriented scripts require a lot of planning. You need to plan your objects and their properties and what they can do. Your objects need to cover all their responsibilities without encroaching on the responsibilities of other objects. For complicated projects, you might have to do some model building and testing before you can feel reasonably confident that your project plan includes all the objects it needs.

Developing object-oriented scripts includes the following procedures:

Choose the objects.

Choose the properties and methods for each object.

Create the object and put it to work.

Choose the objects for your script

Your first task is to develop the list of objects needed for your programming project. If you're working alone and your project is small, the objects might be obvious. However, if you’re working on a large, complex project, selecting the list of objects can be more difficult.

For example, if your project is developing the software that manages all the tasks in a bank, your list of possible objects is large: account, teller, money, checkbook, wastebasket, guard, vault, alarm system, customer, loan, interest, and so on. But, do you need all those objects? What is your script going to do with the wastebasket in the front lobby? Or the guard? Well, perhaps your script needs to schedule shifts for the guards.

When you’re planning object-oriented programs, the best strategy for identifying your objects is to list all the objects you can think of — that is, all the nouns that might have anything at all to do with your project. Sometimes programmers can take all the nouns out of the project proposal documentation to develop a pretty comprehensive list of possible objects.

After you create a long list of possible objects, your next task is to cross off as many as possible. You should eliminate any duplicates, objects that have overlapping responsibilities, and objects that are unrelated to your project.

For example, if your project relates to building a car, your car project probably needs to have objects for every part in the car. On the other hand, if your project involves traffic control in a parking garage, you probably need only a car object that you can move around; the car’s parts don't matter for this project.

Select properties and methods for each object

When you have a comprehensive list of objects, you can begin to develop the list of properties for each object. Ask yourself what you need to know about each object.

For example, for a car repair project, you probably need to know things like when the car was last serviced, its repair history, any accidents, details about the parts, and so on. For a project involving parking garage traffic, you probably need to know only the car’s size. How much room does the car take up in the parking garage?

You need to define the responsibilities of each object, and each object needs to be independent. It needs methods for actions that handle all of its responsibilities. For example, if one of your objects is a bank account, you need to know what a bank account needs to do.

Well, first, it needs to be created, so you can define an openNewAccount method. It needs to accept deposits and disburse withdrawals. It needs to keep track of the balance. It needs to report the balance when asked. It might need to add interest to the account periodically. Such activities come to mind quickly.

However, a little more thought, or perhaps testing, can reveal activities that you overlooked. For example, the account stores information about its owner, such as name and address. Did you remember to include a method to update that information when the customer moves? It might seem trivial compared to moving the money around, but it won't seem trivial if you can't do it.

Create and use an object

After you decide on the design of an object, you can create and then use the object. The steps for creating and using an object are as follows:

Write the class statement.

The class statement is a PHP statement that is the blueprint for the object. The class statement has a statement block that contains PHP code for all the properties and methods that the object has.

Include the class in the script where you want to use the object.

You can write the class statement in the script itself. However, it’s more common to save the class statement in a separate file and use an include statement to include the class at the beginning of the script that needs to use the object.

Create an object in the script.

You use a PHP statement to create an object based on the class. This is called instantiation.

Use the new object.

After you create a new object, you can use it to perform actions. You can use any method that is inside the class statement block.