#37047 [Opn->Csd]: static executes after return

From: Date: Mon, 23 Nov 2009 13:03:04 +0000
Subject: #37047 [Opn->Csd]: static executes after return
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-3309@lists.php.net to get a copy of this message
ID: 37047 Updated by: vrana@php.net Reported By: karoly at negyesi dot net -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: Irrevelant PHP Version: 5.1.2 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2007-08-23 01:14:31] 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 :) ------------------------------------------------------------------------ [2007-08-20 12:22:02] 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-20 10:42:39] 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. ------------------------------------------------------------------------ [2006-04-12 08:31:01] 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". ------------------------------------------------------------------------ [2006-04-11 22:44:27] 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... ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/37047 -- Edit this bug report at http://bugs.php.net/?id=37047&edit=1

« previous php.doc.bugs (#3309) next »