Creating Constructors
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 ...
Get PHP Advanced for the World Wide Web: Visual QuickPro Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.