ID: 48135 User updated by: wizanda at yahoo dot co dot uk Reported By: wizanda at yahoo dot co dot uk Status: Bogus Bug Type: Feature/Change Request Operating System: all PHP Version: 5.2.9 New Comment:
Since this is a major issue, still waiting for some response, though I've now changed most static questions to be returned before it overfills, there are numerous PHP examples and projects all not knowing this. Please test it more carefully as it basically means PHP is a memory hog, when it doesn't need to be. Previous Comments: ------------------------------------------------------------------------ [2009-05-04 12:23:29] wizanda at yahoo dot co dot uk There are two scripts there... The first has shown multiple copies on a fresh install of WAMP, and on my webserver. This same principle is similar to a singleton pattern and can be also tested in these. Even on a single variable with no array, if multiple copies of a class instance are called; When we var_dump() the static variable, it has multiples of the class object, and thus its not single.... To make it single, as a solution to the static variables holding to much data, the second script shows that if it's set we return it; Thus only allowing the one copy of the class object, within the static variable. ------------------------------------------------------------------------ [2009-05-04 11:28:06] [email protected] I can't reproduce this, the output I get is as follows: scott-mbp:tmp scott$ /usr/bin/php test.php count<br />array(1) { ["Funky"]=> object(Funky)#1 (0) { } } ------------------------------------------------------------------------ [2009-05-04 11:12:27] wizanda at yahoo dot co dot uk Found that if you return it first when it's set, it won't multiple fill the static var, yet this is also the same as a singleton pattern, so it looks as if those are also mass storing information. <?php class Funky{} class Monkey{} Class etc{} function collectClass($name){static $handlers; if(isset($handlers[$name])){$handle = $handlers[$name]; return $handle;}else{$handlers[$name] = new $name(); echo 'count<br />';} var_dump($handlers); $handle = $handlers[$name]; return $handle;} collectClass('Funky'); collectClass('Funky'); collectClass('Funky'); ?> ------------------------------------------------------------------------ [2009-05-04 09:45:34] wizanda at yahoo dot co dot uk Thank you for the reply, here is a better example and a rephrase of the problem... <?php class Funky{} class Monkey{} Class etc{} function collectClass($name){static $handlers; if(!isset($handlers[$name])){$handlers[$name] = new $name(); echo 'count<br />';} var_dump($handlers); $handle = $handlers[$name]; return $handle;} collectClass('Funky'); collectClass('Funky'); collectClass('Funky'); ?> On doing this i realized its not so much isset is broken sorry, as if we test the occurrences of the count, we find it runs once...Yet if we var_dump() the static var, it has 3 copies of something that it has only collected once and so shouldn't it only be stored once? Else its amassing tons of data in a large system? ------------------------------------------------------------------------ [2009-05-04 00:42:28] [email protected] Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. We need a self contained script that we can run, though it sounds like you just need. !isset($handler, $handler[$name]) To check both the array and the key. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/48135 -- Edit this bug report at http://bugs.php.net/?id=48135&edit=1
