Page 205 - Beginning PHP 5.3
P. 205
Chapter 8: Objects
simplify the search for bugs and makes the addition of features a relatively painless task. Modularity is
particularly important when working on large, complex applications.
Applications written using OOP are usually relatively easy to understand. Because an object - oriented
approach forces you to think about how the code is organized, it ’ s a lot easier to discover the structure
of an existing application when you are new to the development team. What ’ s more, the object - oriented
design of the application gives you a ready - made framework within which you can develop new
functionality.
On larger projects, there are often many programmers with varying skill levels. Here, too, an object -
oriented approach has significant benefits over procedural code. Objects hide the details of their
implementation from the users of those objects. Instead of needing to understand complex data
structures and all of the quirks of the business logic, junior members of the team can, with just a little
documentation, begin using objects created by senior members of the team. The objects themselves are
responsible for triggering changes to data or the state of the system.
Now you have an idea of the advantages of object - oriented applications. You ’ re now ready to learn the
nitty - gritty of classes and objects, which you do in the next few sections. By the end of this chapter, you ’ ll
probably come to see the benefits of the OOP approach for yourself.
Understanding Basic OOP Concepts
Before you start creating objects in PHP, it helps to understand some basic concepts of object - oriented
programming. In the following sections, you explore classes, objects, properties, and methods. These are
the basic building blocks that you can use to create object - oriented applications in PHP.
Classes
In the real world, objects have characteristics and behaviors. A car has a color, a weight, a manufacturer,
and a gas tank of a certain volume. Those are its characteristics. A car can accelerate, stop, signal for a
turn, and sound the horn. Those are its behaviors. Those characteristics and behaviors are common to all
cars. Although different cars may have different colors, all cars have a color.
With OOP, you can model the general idea of a car — that is, something with all of those qualities — by
using a class . A class is a unit of code that describes the characteristics and behaviors of something, or of
a group of things. A class called Car , for example, would describe the characteristics and behaviors
common to all cars.
Objects
An object is a specific instance of a class. For example, if you create a Car class, you might then go on to
create an object called myCar that belongs to the Car class. You could then create a second object,
yourCar , also based on the Car class.
Think of a class as a blueprint, or factory, for constructing an object. A class specifies the characteristics
that an object will have, but not necessarily the specific values of those characteristics. Meanwhile, an
object is constructed using the blueprint provided by a class, and its characteristics have specific values.
167
9/21/09 9:03:31 AM
c08.indd 167
c08.indd 167 9/21/09 9:03:31 AM