views:

43

answers:

2

I have an Object

    (
        [id] => 1
        [parent_id] => 0
        [result:Database:private] => 
        [db:Database:private] => mysqli Object
            (
                [affected_rows] => 0
                ...
            )

    )

Obviously, the Object has inherited the 'db' and 'result' properties of the parent Database class.

unset($object->result) nor unset($object->result:Database) nor unset($object->result:Database:private) work.

How could I unset those properties when they are no longer needed (i.e. when the object properties are about to be output)?

Is it a generally a good idea to have a database object as an inherited property of other classes (extend one Database class with all other classes that use database connections)?

+1  A: 

Try $object->result = null

Is it a generally a good idea to have a database object as an inherited property of other classes (extend one Database class with all other classes that use database connections)?

No. It is best that your class have one responsibility. Keep your database connections in a class or set of classes whose responsibility is to read and write data from the database. Your other domain objects should handle their own responsibilities without being crowded by SQL and database code - even if it is inherited.

You may also want to read up on the concept of "favor composition over inheritance."

Scott Saunders
$object->result = null will just remove the reference to DB which will continue to exist in the memory. unset() removes object from the memory, he cannot use unset() because DB is inherited in other classes too.
Ivo Sabev
Unfortunately, neither $object->result = null, nor unset($object->result) works. $object->result = null only sets new property 'result'.
vbklv
+1  A: 

If you are using unset() only in one object you are not unsetting the object, because it exists in the memory somewhere and what you have in your object is just a reference. The same reference is used by all you other objects holding this DB object. The only thing you can do is to set this reference to equal NULL, but you are not freeing any memory since the DB is still there used by other objects.

Generally I would not worry about unsetting the DB object as you will not get any performance boost. PHP will automatically garbage collect its objects when the execution ends.

Yes, it is a good idea to have one static DB object or inherit it in your classes that are using the database.

Ivo Sabev
The purpose of unsetting the database object inherited properties is not to free memory, but to make the child object more manageable to iterations for example. I am extending the Database class with all other classes which require database IO, because I find it easy to just do $this->query($query) and also Eclipse does not like the $this->db->query($query) syntax. I am glad you find this approach a good idea.Мерси много за помощта приятелю ;)
vbklv
What do you mean by more manageable? Also all classes that inherit the DB is a good idea to be specific to only doing database manipulations. Пак заповядай :)
Ivo Sabev
For example: Item class with many properties, extends the Database class, so the Item class can use any database methods. When an object of class Item is about to be output, I'd like to strip the object of any inherited properties and use iteration on the Item object.
vbklv