avoid object creation...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
exist a way to avoid an object creation on error??
i have this on php5.0.5

public function __construct(){
                      $this->DBpass) = func_get_args();

                $this->res =
                        echo odbc_error()."<br>\n".odbc_errormsg();
                /// here put something to avoid contruction of the

in the script i have this:

$db = new DbCon('dsn','user','pass');
  //do some
  //do some

Re: avoid object creation...

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 =
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.

Site Timeline