php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37383 scope of static variable in class member function exists in all object instance
Submitted: 2006-05-09 15:51 UTC Modified: 2006-05-09 18:11 UTC
From: patrick at factorylabs dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2006-05-09 15:51 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-09 18:11 UTC] helly@php.net
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


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 02:01:29 2024 UTC