note 20500 added to language.oop

From: Date: Fri, 05 Apr 2002 13:18:00 +0000
Subject: note 20500 added to language.oop
Groups: php.notes 
Request: Send a blank email to php-notes+get-28665@lists.php.net to get a copy of this message
The returned result from a constructor cannot be accessed: the result of the call to new is the instantiated object. So error handling in a constructor is problematic. There are a few ways I can think of handling this: 1) call exit() or die() in the constructor -- not good if you want to recover and carry on somehow 2) don't put code that can fail in the constructor, put it in an initialisation function instead which can return an error result -- somewhat defeats the purpose of a constructor (it shouldn't really be possible to construct an object with uninitialised state) 3) when an error occurs, save that state inside the object to be returned using isError and getError functions. Ideally all other functions should refuse to do their job if an error occurred during construction. I guess this is one of the reasons that Java has exceptions. -- http://www.php.net/manual/en/language.oop.php http://master.php.net/manage/user-notes.php?action=edit+20500 http://master.php.net/manage/user-notes.php?action=delete+20500 http://master.php.net/manage/user-notes.php?action=reject+20500

« previous php.notes (#28665) next »