13.8 Classes and Objects
PHP 5 provides comprehensive
and robust support for object-oriented programming. If
you've never heard of object-oriented programming,
then you don't need to use any of these fancy
features. But if you're coming to PHP from a
language such as Java, you can structure your code in familiar ways.
You can create interfaces; abstract classes; public, private, and
protected properties and methods; constructors and destructors;
overloaded property accessors and method dispatchers; and plenty of
other OO goodies.
Chapter 2 of Upgrading to PHP 5 by Adam
Trachtenberg (O'Reilly), lays out the many
object-related changes in PHP 5. The PHP Manual covers classes and
objects at
http://www.php.net/manual/language.oop.php.
13.8.1 Object Basics
An object, in the programming world, is a
structure that combines data about a thing (such as the ingredients
in an entree) with actions on that thing (such as preparing the
entree). Using objects in a program provides an organizational
structure for grouping related variables and functions together.
Some words to know when working with objects are defined in the
following list:
- Class
-
A template or recipe that describes the variables and functions for a
kind of object. For example, an Entree class would
contain variables that hold its name and ingredients. The functions
in an Entree class would be for things such as
cooking the entree, serving it, and determining whether a particular
ingredient is in it.
- Method
-
A function defined
in a class is called a method.
- Property
-
A variable
defined in a class is called a property.
- Instance
-
An
individual usage of a class. If you are serving three entrees for
dinner in your program, you would create three instances of the
Entree class. While each of these instances is
based on the same class, they differ internally with different
properties. The methods in each instance contain the same
instructions, but probably produce different results because they
each rely on the particular property values in each instance.
Creating a new instance of a class is called
"instantiating an object."
- Constructor
-
A special
method that is automatically run when an object is instantiated.
Usually, constructors set up object properties and do other
housekeeping that makes the object ready for use.
- Static method
-
A special
kind of method that can be called without instantiating a class.
Static methods don't depend on the property values
of a particular instance. PEAR DB uses a static method to create a
database connection.
13.8.2 Creating a New Object
PEAR DB uses
a static method to create a new object
instance for you to use:
$db = DB::connect($dsn);
This calls the connect(
) method defined in
the DB class. The connect( )
method is a static method: nothing in connect( )
depends on a specific instance of the DB class. The
classname::method( ) syntax is how you call a
static method. When you see two colons in a function name like that
in a PHP program, think "static method
call."
The other way to create a new object is with the
new operator:
$dinner = new Entree( );
This makes the variable $dinner an instance of the
class Entree. To pass arguments to a
class's constructor, put them in the parentheses:
$dinner = new Entree('Chinese','spicy');
13.8.3 Accessing Properties and Methods
The ->
("arrow")
operator,
composed of a hyphen and a greater-than sign, is your road to the
properties (variables) and methods (functions) inside an object. To
access a property, put the arrow after the object's
name and put the property after the arrow:
print $dinner->price;
$todays_fat = $todays_fat + $dinner->fat;
print 'To eat: '. strtoupper($dinner->name);
To call a method, put the method name after the arrow, followed by
parentheses:
$dinner->prepare( );
$ingredients = $dinner->get_ingredients( );
You can pass arguments to a method just like a regular function:
$has_pineapple = $dinner->contains('Pineapple');
$dinner->add_ingredient('Ginger Root');
$dinner->serve('Alice','Bob','Charlie');
Note that the arrow operator used to access properties and methods is
different than the operator-separating array keys and values in
array( ) or foreach( ). The
array arrow has an equals sign: =>. The object
arrow has a hyphen: ->.
|