note 20500 deleted from language.oop by vincent
| From: | vincent@php.net | Date: | Thu, 11 Sep 2003 19:59:47 +0000 |
| Subject: | note 20500 deleted from language.oop by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-56204@lists.php.net to get a copy of this message | ||
Note Submitter: ashley@dcs.warwick.ac.uk
----
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.