TechniqueAssign the new object to the $this variable: <?php class PHP_QA { var $name; function std_response () { print "Sorry " . $this->name; print ", That's a feature not a bug\n"; } } class BUG { var $type; function BUG ($type) { if ($type == "Invalid Bug") { $this = &new PHP_QA; return; } $this->type = $type; } function std_response () { print "Wow, thanks for reporting a " . $this->type; print " error!!"; } } ?> CommentsIf you want to return a different object from a constructor, you must assign the $this variable the new object. The $this variable is the default return value from a constructor; it contains a pointer to the methods and properties of the current class. Overriding the $this variable enables you to change the object returned from the constructor. Note, however, that you lose all the current class's methods and properties when you assign a new value to the $this variable. |