#37383 [Opn->Bgs]: scope of static variable in class member function exists in all object instance

From: Date: Tue, 09 May 2006 18:11:17 +0000
Subject: #37383 [Opn->Bgs]: scope of static variable in class member function exists in all object instance
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-96863@lists.php.net to get a copy of this message
ID: 37383 Updated by: helly@php.net Reported By: patrick at factorylabs dot com -Status: Open +Status: Bogus Bug Type: Scripting Engine problem -Operating System: Windows +Operating System: * -PHP Version: 5.1.4 +PHP Version: * New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Previous Comments: ------------------------------------------------------------------------ [2006-05-09 15:51:20] patrick at factorylabs dot com Description: ------------ Given a member function of a class that has a static variable; the value of the static variable, when changed, is propagated to all object instances of the class. (note: may be a duplicate of #16245) The code below is strictly intended to demonstrate this issue (having a static variable in a constructor is pointless). Note that removing the static key word from the $bSwitch declaration causes the $bSwitch variable to operate with a scope that is unique to the instance of the class. Reproduce code: --------------- define('endl', "\n"); class CMyWidget { public function __construct() { static $bSwitch = false; echo '$bSwitch = ' . ($bSwitch ? 'true' : 'false') . endl; if($bSwitch === false) $bSwitch = true; } } $oTest1 = new CMyWidget(); $oTest2 = new CMyWidget(); Expected result: ---------------- $bSwitch = false $bSwitch = false Actual result: -------------- $bSwitch = false $bSwitch = true ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=37383&edit=1

« previous php.bugs (#96863) next »