#27724 [NEW]: static variables do not behave like expected
| From: | leonel at nied dot unicamp dot br | Date: | Fri, 26 Mar 2004 20:20:13 +0000 |
| Subject: | #27724 [NEW]: static variables do not behave like expected | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-56923@lists.php.net to get a copy of this message | ||
From: leonel at nied dot unicamp dot br
Operating system: Fedora Core1
PHP version: 5.0.0RC1
PHP Bug Type: Class/Object related
Bug description: static variables do not behave like expected
Description:
------------
I tried to implement a class with the Singleton pattern. However, static
variables do not seem to tie to the class, but rather to the instance of
the class.
Looking at bug 16245
http://bugs.php.net/bug.php?id=16245&edit=2
I see that Mr. Flying and I have sort of 'exchanged' bugs. He got the
result I expected and I got the result he expected.
Notice 2 things: first, he assigned a primitive type to his variable,
whereas my variable holds an object; second, his variable is declared
inside the function, and mine is declared inside the class.
Reproduce code:
---------------
<?php
class IntegerFactory {
private static $int = NULL;
function __construct() {}
public function getInstance() {
if ( !isset($this->int) ) {
echo 'null !', "\n"; $this->int = new IntegerFactory;
}
return $this->int;
}
}
$tmp1 = new IntegerFactory;
$d1 = $tmp1->getInstance();
var_dump($d1);
$tmp2 = new IntegerFactory;
$d2 = $tmp2->getInstance();
var_dump($d2);
$d3 = $tmp1->getInstance();
var_dump($d3);
?>
Expected result:
----------------
null !
object(IntegerFactory)#2 (0) {
}
object(IntegerFactory)#2 (0) {
}
object(IntegerFactory)#2 (0) {
}
Actual result:
--------------
null !
object(IntegerFactory)#2 (0) {
}
null !
object(IntegerFactory)#4 (0) {
}
object(IntegerFactory)#2 (0) {
}
--
Edit bug report at http://bugs.php.net/?id=27724&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=27724&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=27724&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=27724&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=27724&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=27724&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=27724&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=27724&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=27724&r=support
Expected behavior: http://bugs.php.net/fix.php?id=27724&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=27724&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=27724&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=27724&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=27724&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=27724&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=27724&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=27724&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=27724&r=float