self-destructing objects in php5?
Posted
by user151841
on Stack Overflow
See other posts from Stack Overflow
or by user151841
Published on 2010-05-04T19:31:38Z
Indexed on
2010/05/04
19:38 UTC
Read the original article
Hit count: 144
I am working on a class in php that is basically an interface to a database row. I wanted to create a delete()
method that would 1. delete the database row and 2. destroy the instance of itself so that further attempts to manipulate the row via the object would throw warnings.
Doing some googling, it seems that, in php5, it's not possible for an object to unset itself. http://bugs.php.net/bug.php?id=36971
In fact they discuss the very situation I was wanting to do :(
So how should I proceed? I could make boolean flag as a class property, for whether the row still exists, and have each operation check that flag and throw an error if the row has been deleted. This maintains the oo structure of code, so I would have
$objDbRow->delete();
But then I have to put checks at the beginning of each method.
Or, I could implement a __destruct
method that deletes the row. But that would seem counter-intuitive to me; if I saw in code
unset($objDbRow);
All I would suspect that's happening is that the object is being discarded, not that a row is being deleted. So that to me would seem like bad practice.
© Stack Overflow or respective owner