tags:

views:

25

answers:

1

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.

+3  A: 

I would leave the delete() method in place and create an internal flag named active. When the row gets deleted, that flag is set to false.

The flag would get checked before any attempt to access any of the object's data properties. If it is false, return false, throw a warning ... or whatever suits your application's philosophy best.

Pekka