php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Thank you for your help! If the status of the bug report you submitted changes, you will be notified. You may return here and check the status or update your report at any time.
The URL for your bug report is: https://bugs.php.net/bug.php?id=48135.
Request #48135 Isset doesn't function as expected
Submitted: 2009-05-03 12:33 UTC Modified: 2009-05-11 09:37 UTC
From: wizanda at yahoo dot co dot uk Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.9 OS: all
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-04 00:42 UTC] scottmac@php.net
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.
 [2009-05-04 09:45 UTC] 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 11:12 UTC] 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 11:28 UTC] scottmac@php.net
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 12:23 UTC] 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-11 09:37 UTC] wizanda at yahoo dot co dot uk
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 21:01:36 2024 UTC