php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #37047 static executes after return
Submitted: 2006-04-11 22:15 UTC Modified: 2009-11-23 13:03 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: karoly at negyesi dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.1.2 OS: Irrevelant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: karoly at negyesi dot net
New email:
PHP Version: OS:

 

 [2006-04-11 22:15 UTC] karoly at negyesi dot net
Description:
------------
The code has been tested on PHP 4.3, 4.4, 5.0 and 5.1 various minors.

Reproduce code:
---------------
function storage($key) {
  static $storage = array('a' => array('x', 'y'));
  return $storage[$key];
  static $storage = array('x', 'y'); // comment this out to see expected result
}
var_dump(storage('a'));

Expected result:
----------------
array(2) {
  [0]=>
  string(1) "x"
  [1]=>
  string(1) "y"
}


Actual result:
--------------
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-11 22:21 UTC] tony2001@php.net
Static variables are resolved in compile time.
Expected behaviour.
 [2006-04-11 22:28 UTC] karoly at negyesi dot net
Then this is a docs issue. Because I do not expect anything to be executed after a return...
 [2006-04-11 22:37 UTC] sean@php.net
see: http://php.net/return

"the return() statement immediately ends execution of the current function"

(It's already documented.)

S

 [2006-04-11 22:38 UTC] sean@php.net
Sorry. I misread.

You're right (-:

S

 [2006-04-11 22:44 UTC] karoly at negyesi dot net
Hint. If you doc this please doc everything as well that executes at compile time. It will be a very interesting handbook page...
 [2006-04-12 08:31 UTC] tony2001@php.net
It's the same as
<?php
exit;
class Test { }
?>
The class will be still declared, even though there is an exit statement before the declaration. It doesn't mean that it's "executed", because there is a big difference between "execution" and "compilation".
 [2007-08-20 10:42 UTC] vrana@php.net
If static variables are resolved in compile time then

<?php
function storage($key) {
  return $storage;
  static $storage = array('a' => array('x', 'y'));
}
var_dump(storage('a'));
?>

should give an expected result.
 [2007-08-20 12:22 UTC] derick@php.net
No, there is a difference between definition and assignment. The variable is overwritten by the static-call during compilation. The value is only assigned when the assignment is actually done.

In Jakub's last example, the code never hits the assignment statement.

In the original report, the "static $storage" definition is done twice, and during the 2nd time the original $storage static definition is destroyed. The static fetch in the return statement will therefore not work. I'd say this should be documented...

It wouldn't be hard to add a notice for this though.
 [2007-08-23 01:14 UTC] stas@php.net
Heh, funny thing. What static does is two things:
1. When compiling, create "static" variable entry in function's static symbol table and assign it a value from expression or just null if none supplied (note no code is executed because the value is constant - parser just parses it and puts into symbol table). 
2. When "static" statement is reached in runtime, bind that entry to regular symbol table with the same name.

Now note that when you define static second time, you instantly change the value in static table to new value. Maybe PHP might notify you on that, but right now it's just gives you enough rope :)
 [2009-11-23 13:03 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2009-11-23 13:03 UTC] svn@php.net
Automatic comment from SVN on behalf of vrana
Revision: http://svn.php.net/viewvc/?view=revision&revision=291201
Log: Static resolved in compile-time (bug #37047)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Aug 02 15:00:03 2025 UTC