by
Brian Haveri aka bwh2 | 31 December 2006In the
previous
page I started explaining the importance of OOP and how
to create a basic object. In this and subsequent, we'll
hammer out many of the details.
Within classes, we first define our
variables. In this case, our first variable is
$name. A
handy feature of PHP5 (not PHP4) is increased visibility
control. Our $name variable currently has a visibility of
private. Private visibility means that the variable can only
be accessed via the methods inside the class (like
setName
and getName). If we set the variable visibility to public,
our variable could be accessed externally (without the
methods). The final visibility setting for a variable is
protected, which means that variable access is limited to
parent and inherited classes, which we will discuss more
later.
In PHP4, public is the only visibility option and public
variables are declared using
var instead
of public.
Let's take a look at how variable visibility works by adding
a $location
variable and setting it to public visibility:
So which variable visibility should you
choose? The answer is that it depends. In general, you
should be fine using private. Private is especially useful
because it supports encapsulation - the ability to hide data
and only make it accessible through a given interface. In
OOP, an interface represents the functionality given to a
particular object.
Just as we set a variable's visibility, we can also set
visibility for methods. By default, if we don't set the
visibility of our methods, they will be set to public. For
the sake of time, we will not dig into examples with
different method visibility. Continue on to learn about an
important and useful method, the constructor.
Note about $this: Within methods, we refer to class
variables and other methods using $this->. This can be seen
within the setName and
getName methods. $this is a default
variable created upon instantiation and enables an object to
reference itself.
Note about naming conventions: While
set[PropertyName]
and
get[PropertyName] are popular method names because they
are easily understood, they are not required. You can name
these methods whatever you want.
Now is a good time to look at the constructor method. The
constructor is a method that will automatically be run when
an object is instantiated. In PHP5, we have methods called "Magic
Methods" (i.e. __construct,
__destruct, and a few
others) which begin with two underscores. In PHP4, the
constructor method would be given the same name as the class
itself. So in PHP4, if we have a class named User, our
constructor method would also be named User (not
__construct). Remember, you do not need to call the
constructor method explicitly because it will be
automatically run when an instance of that class is created.
Here's a constructor in action:
Now you might be starting to see one of the benefits of
OOP - much of the code lies in the background. We could
include files containing our classes to hide the heavy duty
code even more. Because the naming conventions of classes
and methods are fairly self explanatory, we won't need to
constantly look back at our included files. Continue reading
to find out how we can use arrays to speed up our process.
|