Page 204 - Beginning PHP 5.3
P. 204
Part II: Learning the Language
What Is Object - Oriented Programming?
So far in this book you ’ ve written code that passes chunks of data from one function to the next — a
technique known as procedural programming . Object - oriented programming takes a different approach.
Objects model the real - world things, processes, and ideas that your application is designed to handle. An
object - oriented application is a set of collaborating objects that independently handle certain activities.
For example, when a house is being constructed, the plumbers deal with the pipes, and the electricians
deal with the wires. The plumbers don ’ t need to know whether the circuit in the bedroom is 10 amps
or 20. They need only concern themselves with their own activities. A general contractor ensures that
each subcontractor is completing the work that needs to be accomplished but isn ’ t necessarily interested
in the particulars of each task. An object - oriented approach is similar in that each object hides from the
others the details of its implementation. How it does its job is irrelevant to the other components of the
system. All that matters is the service that the object is able to provide.
The concepts of classes and objects, and the ways in which you can use them, are the fundamental ideas
behind object - oriented programming. As you ’ ll see, an object - oriented approach gives you some big
benefits over procedural programming.
Advantages of OOP
Let ’ s take a look at some of the advantages of an OOP approach to software development.
To start with, OOP makes it easy to map business requirements to code modules. Because your
application is based on the idea of real - world objects, you can often create a direct mapping of people,
things, and concepts to classes. These classes have the same properties and behaviors as the real - world
concepts they represent, which helps you to quickly identify what code needs to be written and how
different parts of the application need to interact.
A second benefit of OOP is code reuse. You frequently need the same types of data in different places in
the same application. For example, an application that manages hospital patient records might contain a
class called Person . A number of different people are involved in patient care — the patient, the doctors,
the nurses, hospital administrators, and so on. By defining a class called Person that encompasses the
properties and methods common to all of these people, you can reuse an enormous amount of code in a
way that isn ’ t always possible in a procedural programming approach.
What about other applications? How many applications can you think of that handle information about
individuals? Probably quite a few. A well - written Person class can easily be copied from one project to
another with little or no change, instantly giving you all the rich functionality for dealing with
information about people that you developed previously. This is one of the biggest benefits of an object -
oriented approach — the opportunities for code reuse within a given application as well as across
different projects.
Another OOP advantage comes from the modularity of classes. If you discover a bug in your Person
class, or you want to add new features to the class or change the way it functions, you have only one
place to go. All the functionality of that class is contained in a single PHP file. Any parts of the
application that rely on the Person class are immediately affected by changes to it. This can vastly
166
9/21/09 9:03:31 AM
c08.indd 166 9/21/09 9:03:31 AM
c08.indd 166