
In other areas of software development, OO is the norm and function-oriented software is con-
sidered old fashioned. For a number of reasons, most Web scripts are unfortunately still
designed and written using an ad hoc approach following a function oriented methodology.
A number of reasons for this exist. The majority of Web projects are relatively small and
straightforward. You can get away with picking up a saw and building a wooden spice rack
without planning your approach and you can successfully complete the majority of Web soft-
ware projects in the same way because of their small size. However, if you picked up a saw
and attempted to build a house without formal planning, you won’t get quality results, if you
get results at all—the same is true for large software projects.
Many Web projects evolve from a set of hyperlinked pages to a complex application. These
complex applications, whether presented via dialog boxes and windows or via dynamically
generated HTML pages, need a properly thought out development methodology. Object orien-
tation can help you to manage the complexity in your projects, increase code reusability, and
thereby reduce maintenance costs.
In OO software, an object is a unique and identifiable collection of stored data and operations
that operate on that data. For instance, we might have two objects that represent buttons. Even
if both have a label “OK”, a width of 60 pixels, a height of 20 pixels, and any other attributes
that are identical, we still need to be able to deal with one button or the other. In software, we
have separate variables that act as handles (unique identifiers) for the objects.
Objects can be grouped into classes. Classes represent a set of objects that might vary from
individual to individual, but must have a certain amount in common. A class contains objects
that all have the same operations behaving in the same way and the same attributes represent-
ing the same things, although the values of those attributes will vary from object to object.
The noun bicycle can be thought of as a class of objects describing many distinct bicycles with
many common features or attributes—such as two wheels, a color and a size, and operations,
such as move.
My own bicycle can be thought of as an object that fits into the class bicycle. It has all the
common features of all bicycles including a move operation that behaves the same as most
other bicycles’ move—even if it is used more rarely. My bicycle’s attributes have unique values
because my bicycle is green, and not all bicycles are that color.
Polymorphism
An object-oriented programming language must support polymorphism, which means that dif-
ferent classes can have different behaviors for the same operation. If for instance we have a
class car and a class bicycle, both can have different move operations. For real-world objects,
Object-Oriented PHP
C
HAPTER 6
6
OBJECT-ORIENTED
PHP
149
08 7842 CH06 3/6/01 3:34 PM Page 149
Comentarios a estos manuales