|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-05-03 12:33 UTC] wizanda at yahoo dot co dot uk
Description:
------------
Since PHP 5 most PHP systems will be storing masses of data, due to isset and empty no longer assessing the full variable, yet running code once for each item within it.
Reproduce code:
---------------
---
From manual page: function.isset
---
function &loadHandler($name){static $handler;
if(!isset($handler[$name])){$FileName = "core/core.".$name.".php";
require_once $FileName;
$ClassName = "SmartyCore_".$name;
$handler[$name] = new $ClassName();}
$classes = $handler[$name];
return $classes;}
Expected result:
----------------
This should check $handler, see if it's empty the first time; check $name and see this is an empty array, attempt to fill it and thus setting the $handler as being a container for the array and then check for each $name as a separate array within it.
Actual result:
--------------
What this appears to be doing is checking $handler and running it, then checking $name and running it... So when you var_dump the $handler it has multiple copies of the variable set.
This can be slightly resolved by making the array set the original $handler variable, which reduces the copies made.
$handler = array($name => new $ClassName());}
...Yet there should be only one copy set.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 23:00:01 2025 UTC |
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?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'); ?>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) { } }