|  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
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
50 - 32 = ?
Subscribe to this entry?

 [2011-01-06 12:40 UTC] crystal at cyclonephp dot com
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:

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

$testA = new StatTest;

$testB = new StatTest;

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-07 21:52 UTC]
-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]
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:
 [2018-03-15 11:48 UTC]
-Status: Open +Status: Suspended
 [2018-03-15 11:48 UTC]
> 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] <>
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Jan 21 14:03:35 2022 UTC