Back To Class - Who Needs Class, Anyway? (
Page 2 of 9 )
Before beginning, though,
let's make sure that you have a clear idea of the concepts involved
here.
In PHP, a "class" is simply a set of program statements which
perform a specific task. A typical class definition contains both variables and
functions, and serves as the template from which to spawn specific instances of
that class.
Specific instances of a class are referred to as "objects".
Every object has certain characteristics, or "properties", and certain
pre-defined functions, or "methods". These properties and methods of the object
correspond directly with the variables and functions within the class
definition.
Once a class has been defined, PHP allows you to spawn as
many instances of the class as you like. Each of these instances is a completely
independent object, with its own properties and methods, and can thus be
manipulated independently of other objects.
Now, you're probably
wondering whether this is a little redundant, since PHP also allows you to
create your own functions and use them wherever required in your code. And
you're correct, to some extent - if you're only planning to spawn a single
object, a class is redundant and a function will work just as well.
But
there are situations where you need to spawn more than one instance of an object
- for example, two simultaneous database links for two simultaneous queries, or
two shopping carts. In such a situation, classes are preferred, since each
instance of the class comes with its own variables and functions, and thus can
be manipulated with affecting other variables within the program.
Classes
also help you keep your code modular - you can define a class in a separate
file, and include that file only in the pages where you plan to use the class -
and simplify code changes, since you only need to edit a single file to add new
functionality to all your spawned objects.{mospagebreak title=Driving Around The
Bend} Let's consider a simple example, which will illustrate this
better.
Take your average everyday car, that four-wheeled
gasoline-chugging monster that you drive to work every day. Can you consider it,
within the framework of OOP, as an "object"?
Why not? After all, every
car has certain characteristics - the colour, the shape, the number of doors,
the hood ornament - which are equivalent to object properties. And every car
comes with certain obvious functions as well - start, stop, turn, accelerate,
decelerate - all of which are equivalent to object methods.
Let's take it
a little further. Every car can, in fact, be considered a subset of the class
Automobile, which defines the basic characteristics and functions of every
automobile on the planet. Once the class Automobile spawns a new car, or
"object", its individual characteristics (color, shape) can be adjusted
independent of other objects.
Now, if you sat down to code this class, it
would probably look something like this:
<?
class Automobile
{
// this is where the properties are defined
var $colour;
var $shape;
var $size;
var $number_of_doors;
var $number_of_seats;
// this is where the functions are defined
function start()
(
// code goes here
)
function stop()
(
// code goes here
)
function accelerate($speed)
(
// code goes here
)
function decelerate($speed)
(
// code goes here
)
}
?>