December 2001
Intermediate to advanced
520 pages
13h 42m
English
A constructor is a function (or method) within a class that has the exact same name as the class itself. The advantage of a constructor is that it will be executed automatically when an instance of the class is created. A constructor could be used to connect to a database, set cookies, or create an HTML header.
The syntax for a constructor is simple:
class ClassName {
function ClassName () {
// Function code.
}
}
With this format, the following code both creates an instance of the object and then calls the ClassName() function, saving a step.
$object = new ClassName();
Remember that constructors work only if the class contains a method with the same name as the class itself (which is why the protocol is to use the same ...
Read now
Unlock full access