NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 singleton/destructor question

David Mintz dmintz at davidmintz.org
Thu Mar 31 22:00:23 EST 2005


On Thu, 31 Mar 2005, Daniel J Cain Jr. wrote:

> I would think that your static $db inside the MyDatabase class is why
> your not having your destructor called prior to the script ending.
>
> When you do:
>
> $outsideDB = MyDatabase::getInstance();
>
> $outsideDB should be a reference to the private static $db inside
> MyDatabase.  So you can unset that in the script, but it only removes
> the reference to the hidden $db.  And being that internal $db still
> references the object you created __destruct() won't be called.

I don't really follow, but maybe that's because I'm a little dense.

>
> It will be called when the script terminates, but I believe the output
> buffers are closed by this point--so echo in the __destruct() won't be seen.

I thought of that and tried writing a string to a file. That worked
whenever echo worked and it failed whenever echo failed.

>
> You could try another method that unsets the $db inside the class to
> prove or disprove my theory.

I added this to the class:

	static function unsetConnection() { self::$db=NULL; }

because PHP didn't much care for unset(self::$db). Then from outside the
class I did:

	MyDatabase::unsetConnection();

at the end of the script, and the destructor did not execute. Can't say
whether that proves or disproves anything since I'm not quite sure I'm on
the right page.

Thanks,

	David
---
David Mintz
http://davidmintz.org/



More information about the talk mailing list