|
Posted by Richard Levasseur on 02/16/06 04:26
You can throw an exception inside the constructor.
try {
$ob = new obj();
} catch(exception $e) { unset($ob) }
if(.....) { }
Though, I remember reading about memory leaks throwing an exception in
a constructor awhile back. When or if it that was fixed, I don't
recall. How it would deconstruct afterwards I don't know, either.
Better form would be to: use a factory or singleton pattern, $conn =
DB::getConnection();
a static method of the object, $conn = new DBConn($dsn);
if($conn->connect()) { ... }
or check some part of the object after created, $conn = new DBConn();
if($conn->isValid()) { .... }
Aside from out of memory errors or something beyond the object's
control, construction should never fail.
[Back to original message]
|