TechniqueGive your constructor function the same name as the class you are calling: <?php class Human { var $name; function Human ($na) { print "Wake up $na, you have been initialized"; $this->name = $na; } } $obj = &new Human('Stephen Hughes'); ?> CommentsIn PHP, a constructor is defined as a method that has the same name (case-sensitive) as the class. The constructor can take arguments, which allows for customization of the object at the initialization time. Also note that if a parent class also has a constructor, the parent class's constructor will not be called when a child class is instantiated . So, you have to do a bit of a manual work. Suppose that you have the following child class to the class Human specified earlier: <?php class SuperHuman extends Human { var $power; function SuperHuman($power) { Human::Human(); $this->$power = $power; } function use_power() { /* this should do something interesting */ } } ?> Here the SuperHuman constructor will be called, but the Human constructor will not automatically be called. So, you have to call it manually using :: notation (see recipe 9.6). |