Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:27539 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55609 invoked by uid 1010); 19 Jan 2007 15:20:24 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 55593 invoked from network); 19 Jan 2007 15:20:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2007 15:20:24 -0000 Authentication-Results: pb1.pair.com header.from=schultz@widescreen.ch; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=schultz@widescreen.ch; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain widescreen.ch from 195.49.42.11 cause and error) X-PHP-List-Original-Sender: schultz@widescreen.ch X-Host-Fingerprint: 195.49.42.11 mail.screenlight.ch FreeBSD 4.8-5.1 (or MacOS X 10.2-10.3) Received: from [195.49.42.11] ([195.49.42.11:54395] helo=screenlight.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/18-03951-7B1E0B54 for ; Fri, 19 Jan 2007 10:20:24 -0500 Received: from [195.49.42.3] (account lars@toolpark.com HELO [192.168.1.168]) by screenlight.ch (CommuniGate Pro SMTP 4.1.8) with ESMTP id 3621566 for internals@lists.php.net; Fri, 19 Jan 2007 16:19:45 +0100 Message-ID: <45B0E1EE.2030102@widescreen.ch> Date: Fri, 19 Jan 2007 16:21:18 +0100 User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: PHP Object-Caching and Reference Counting From: schultz@widescreen.ch (Lars Schultz) Hello there... I am making my debut appearance here hoping that I am not giving offense to anyones well honed php-instincts or programming practices with my feature-request. This is what it comes down to, I guess. A Feature Request for some meta-info on the instantiated PHP-Objects. Specifically I want to know how many references are currently pointing to any one object. Since PHP's Memory Management is based on Reference-Counting this information should already be available "somewhere". Not knowing any php-source-code internals, I can't even guess at the problems which prohibit the implementation of such a feature. Nevertheless I'd like to point out an advantage to the community if the reference-count were available. I have been working on a large project for 6 years now and I got to a point where I'd like to cache certain objects singleton-like. To better explain my problem I provide this small example which surely is error-prone but should get the basic idea across. id = $id; } /** * Returns an instance of the class. * If no object was instantiated with the given $id before, * it will instantiate a new object which will be associated with the given $id. * * It will also check the current memory-usage and if its used up for more than * 80% it will call the collect() method. * * @param int $id * @return MyObject */ public static function getInstance($id){ if ( !isset(self::$instances[$id]) ) { //check the used up memory wether to collect unused objects or not if ( memory_get_usage() > ini_get('memory limit')*0.8 ) self::collect(); self::$instances[$id] = new MyObject($id); } return self::$instances[$id]; } /** * Removes any object from the cache-list which is not referenced anymore. * (safe for the list and the local-reference to it) */ public static function collect(){ foreach( self::$instances as $id => $instance ) { if ( reference_count($instance) < 3 ) unset(self::$instances[$id]); } } } ?> As you can see I boldly used the function reference_count(Object) even though it doesn't exist. This is exactly what I'd need to allow my concept to work. I keep thinking of any other solutions, but up until now, I didn't find any better solution for this problem. Of course I can always ignore the memory usage and hope that it wont hit the limit...but hoping is a rather ethereal and mystic idea which doesn't go well with my preferred down-to-earth 1 plus 1 equals 2 programming. I know that a lot of you may say: Why do need to do that?...and I'd have to think for 3 days and then write for 4 days and would give up. It seems like a nice solution to me and if any of you have an idea as to how I could implement this differently, please feel free to point it out to me. Thanks for reading this far. Lars