Back To Class - Under Construction (
Page 7 of 9 )
It's
also possible to automatically execute a function when the class is called to
create a new object. This is referred to in geek lingo as a "constructor" and,
in order to use it, your class definition must contain a function with the same
name as the class.
For example, if you'd like your car to start
automatically when you create it, add the function Automobile() to your
definition, as show below.
<?
class Automobile
{
// constructor
function Automobile
{
$this->start();
}
function start()
(
// code goes here
)
}
?>
Or, in the table example you just saw, you could define a
default grid and colours for your table when the object is created.
<?
class Table
{
// constructor
function Table()
{
$this->rows = 4;
$this->columns = 5;
$this->bcolor = "black";
$this->fcolor = "white";
$this->font = "Times New Roman";
}
// other functions
?>
And now, when you create a new Table object like this,
without defining any parameters,
<?
// first table
$alpha = new Table;
$alpha->drawTable();
?>
you'll get a default 4x4 black and white grid. Try it and see
for yourself!