|
Object oriented PHP primer |
|
This tutorial gives a brief, very simplistic summary on using objects in
PHP. It is in no
way a complete reference of the capabilities of OOP in PHP.
An object in programming can be anything really, it is just as general
as any 'object'. It simply describes a thing like that object over there.
We'll use people for an example of objects. An object can do things and remember (store)
things.
Most objects do both. People can do things and remember things. Most people do both.
The things an object can do are called 'methods'. The things that objects can remember
are called 'properties'. Methods may set properties or return a value
or do both. Let's take our people example and create an object out of it.
$him = new person();
Now $him is a new person object. This person has properties that make him unique. Assume
the 'get_hair_color()' method and 'hair_color' property exists.
$hair_color = $him->get_hair_color();
$hair_color = $him->hair_color;
The first part uses the object's method that returns his hair color. This is
the equivilent of asking him what his hair color is. The second line views the
hair_color property directly. If you can't see what color his hair is, you'll have
to ask him. Some objects require that you ask them for a property and do not allow
you to access them direclty. This would be in the 'class definition' or definition
that defines an object.
With Thyme's calendar object, all properties can be retrieved directly. E.g.
$month = $cal->month;
Setting an object's properties works much the same way. Sometimes you can direclty
set them and sometimes you have to use a method. This, again, is defined in the
object's class definition. To put it in our person example context, you can tell
someone their hair is green, but until they dye their hair, it is not actually
green.
$him->hair_color = 'green';
Now he is running around telling everyone his hair is green. But he hasn't actually
dyed his hair. Let's die his hair green instead of just telling him his hair
is green.
$him->dye_hair('green');
With Thyme's calendar object, all properties must be set by using the 'set()' method.
This takes two arguments, the property you wish to set, and the value that you wish
to set it to. E.g.
$cal->set("hour_format", 12);
For a complete list of Thyme's properties and methods, see the Thyme API Reference.
|