Re: Variable Reference Counters

From: Date: Fri, 06 Sep 2002 13:39:26 +0000
Subject: Re: Variable Reference Counters
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-88003@lists.php.net to get a copy of this message
The function get_reference_count() could be helpful with garbage collection. For example most people using Java do not know when the garbage collection fails like collections, double linked lists and such. I guess your point was to addresses exactly this circumstance. If so that function would enable advanced programmers to support garbage collection for huge memory consumting php applications. Therefor i am +1. But what about handling statics and constants? marcus At 11:43 06.09.2002, Tom Oram wrote:
Hi, In the Zend Engine I assume each (referenced) variable has some sort of reference counter so that when the last reference is lost the memory is freed up, am I right? If I am has anyone ever suggested or are there any plans to make this available to the php scripts, this goes for the object pointers (or whatever you call them) in the Zend 2 Engine. This would be extremely useful if you had a object loader which either loaded a object from the database or returned the already loaded object, but cleanup up automatically (if that made any sense). Here is some code to explain: <? // This is Zend Engine 1 Code /* * Object to be loaded */ class MyObject {
    var $id;
    var $someParam;
} /* * Object loader class */ class ObjectLoader {
    var $objects = array();
    // Example load function
    function &loadFromDatabase($id) {
        $res = mysql_query("
                            select id,someParam
                            from database
                            where id=$id
                            ");
        $row = mysql_fetch_row($res);
        $obj = new MyObject;
        $obj->id = $row[0];
        $obj->someParam = $row[1];
        return $obj;
    }
    function &loadById($id) {
        // Check if the object is already loaded, if not load it
        if (!isset($objects[$id]))
            $this->objects[$id] =& $this->loadFromDatabase($id);
        // Return the object
        return $this->objects[$id];
    }
    // Session serialise handler
    function __sleep() {
        foreach ($this->objects as $id => $objCpy) [
            // Check if this is the last reference to the object
            if (get_reference_count($this->objects[$id])) {
                // there are no other references to this object
                // so it is unused so we can clear up
                unset($this->objects[$id]);
            }
        }
    }
} ?> With this code you could create an object loader and store it in session then load objects from it, (store the references in session if you want), then get as may references to the same objects just by calling koadById() and you could be happy that when you have finished with an object it won't stay in you session causing bloat! Hope you understood that. Obviously if these reference counters don't exist then this has been a wasted post but I think its a nice idea. Cheers Tom -- *************************************************** Tom Oram SCL Computer Services URL http://www.scl.co.uk/ *************************************************** -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.dev (#88003) next »