avoid object creation...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roco3D

    avoid object creation...

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

    public function __construct(){
    @list($this->DSN,
    $this->DBuser,
    $this->DBpass) = func_get_args() ;

    $this->res =
    odbc_connect($t his->DSN,$this->DBuser,$this->DBpas$
    if(!is_resource ($this->res)){
    echo odbc_error()."< br>\n".odbc_err ormsg();
    /// here put something to avoid contruction of the
    object
    }
    }

    in the script i have this:

    $db = new DbCon('dsn','us er','pass');
    if(is_object($d b)){
    //do some
    }else{
    //do some
    }

  • Richard Levasseur

    #2
    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 =
    DB::getConnecti on();
    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.

    Comment

    Working...