php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53666 local static variables in methods tied to the object instance
Submitted: 2011-01-06 12:40 UTC Modified: 2018-03-15 11:48 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: crystal at cyclonephp dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 5.3.4 OS: linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-01-06 12:40 UTC] crystal at cyclonephp dot com
Description:
------------
The inital value of a static variable in a non-static method should take care 
about the current instance. The initialization should be done when a method of 
the instance is called, and not only when the method is first called in the 
script. The local static variable should be interpreted as an object variable 
that is visible only in the declaring method.

Test script:
---------------
<?php

class StatTest {
	
	function statvartest() {
		static $var = 'initial';
		echo $var."\n";
		if ($var == 'initial') {
			$var = 'changed';
		}
	}
}

$testA = new StatTest;
$testA->statvartest();
$testA->statvartest();

$testB = new StatTest;
$testB->statvartest();
$testB->statvartest();



Expected result:
----------------
initial
changed
initial
changed


Actual result:
--------------
initial
changed
changed
changed


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-07 21:52 UTC] cataphract@php.net
-Summary: local static variables in methods handled incorrectly +Summary: local static variables in methods tied to the object instance -Type: Bug +Type: Feature/Change Request
 [2011-01-07 21:52 UTC] cataphract@php.net
Why? At most this is a (bc breaking) feature request. Function static variables have always been tied only with the function itself.
 [2011-01-07 22:13 UTC] crystal at cyclonephp dot com
Because this way local static variables can't be used eg. for caching method 
return values. I think it's a very important use case. I also don't think that it 
would cause severe bc issues.
 [2011-11-13 21:24 UTC] normandiggs at gmail dot com
Please, fix it. It's very weird behavior.
Another one example: http://codepad.viper-7.com/jKkwzF
 [2018-03-15 11:48 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-03-15 11:48 UTC] cmb@php.net
> Please, fix it. It's very weird behavior.

In my opinion, tying *static* variables to *dynamic* object
instances would be rather surprising.  Furthermore considering the
BC break, this feature certainly would require the RFC process[1].
Anybody is welcome to start it!  For the time being I'm suspending
this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Jul 24 02:01:27 2019 UTC