Specifications

One thing to remember is that PHP does not support function overloading, which means that
you can only provide one function with any particular name, including the constructor. (This is
a feature supported in many OO languages.)
Instantiation
After we have declared a class, we need to create an objecta particular individual that is a
member of the classto work with. This is also known as creating an instance or instantiating
a class. We create an object using the new keyword. We need to specify what class our object
will be an instance of, and provide any parameters required by our constructor.
The following code declares a class called classname with a constructor, and then creates three
objects of type
classname:
class classname
{
function classname($param)
{
echo “Constructor called with parameter $param <br>”;
}
}
$a = new classname(“First”);
$b = new classname(“Second”);
$c = new classname();
Because the constructor is called each time we create an object, this code produces the follow-
ing output:
Constructor called with parameter First
Constructor called with parameter Second
Constructor called with parameter
Using Class Attributes
Within a class, you have access to a special pointer called $this. If an attribute of your current
class is called $attribute, you refer to it as $this->attribute when either setting or access-
ing the variable from an operation within the class.
The following code demonstrates setting and accessing an attribute within a class:
class classname
{
var $attribute;
function operation($param)
{
Using PHP
P
ART I
152
08 7842 CH06 3/6/01 3:34 PM Page 152