# destroy the current object (PDO)

- Thread: https://externals.io/message/36897
- Original archive: https://news-web.php.net/php.internals/36897
- Started by Olivier Bonvalet on 2008-04-09T08:14:56+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #36897 — Olivier Bonvalet — 2008-04-09T08:14:56+00:00

URL: https://externals.io/message/36897#36897

Hello,

I would like to correct a little "problem" with : when an error occured 
in the constructor
(a connection error for example) no object is returned. Internally, the 
object is created
then destroyed (via "ZVAL_NULL(object);" statement).

The problem is when the PDO class is derived and the error catched in 
the "child" constructor :
$this is destroyed, but the object still exists in memory (and the 
__destruct will be called at the
end of the script).

Is there a way here to really destroy the object ?

I submit a bug report (44669), but C. Rodriguez seems to don't agree 
with me. So I would like
to correct the problem myself but... I can't :D

I join here a better example of this behaviour :
<?php
class PDOtest extends PDO
{
    public function __construct( $dsn, $user, $password )
    {
        try {
            parent::__construct( $dsn, $user, $password );
        } catch( PDOException $e ) {
            echo 'there is an error... but continue the script', PHP_EOL;
        }
    }

    public function __destruct()
    {
        echo 'DESTRUCT ::: If called, then the object was existing', 
PHP_EOL;
    }
}

echo 'CONSTRUCT ::: ';
$test = new PDOtest( 'mysql:host=localhost;dbname=test', 'baduser', 
'orpass' );
echo PHP_EOL;

echo 'Here you can see that the object is "destroyed" : ';
var_dump( $test );

echo 'But force to null, to be sure.', PHP_EOL;
$test = NULL;

echo PHP_EOL, 'SOME WORKS', PHP_EOL;
?

---

## #36917 — johannes@php.net — 2008-04-10T12:17:40+00:00

URL: https://externals.io/message/36897#36917 · In reply to: #36897

>     public function __construct( $dsn, $user, $password )
>     {
>         try {
>             parent::__construct( $dsn, $user, $password );
>         } catch( PDOException $e ) {
>             echo 'there is an error... but continue the script', PHP_EOL;
>         }
>     }

The correct thing is to either re-throw the PDOException or use your own
exception and throw that.

       } catch( PDOException $e ) {
           echo 'there is an error... but continue the script', PHP_EOL;
           throw $e;
       }

Everything else is broken on oyur side, no internals@ issue.

johannes
