php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #7553 RFC: Uplevel Block structure
Submitted: 2000-10-31 13:14 UTC Modified: 2010-12-01 15:50 UTC
Votes:7
Avg. Score:4.3 ± 1.2
Reproduced:6 of 6 (100.0%)
Same Version:4 (66.7%)
Same OS:4 (66.7%)
From: clcollie at mindspring dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4.0.3pl1 OS: All
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:
MUST BE VALID
Solve the problem:
18 - 11 = ?
Subscribe to this entry?

 
 [2000-10-31 13:14 UTC] clcollie at mindspring dot com
Request For Comments: The Uplevel block structure
--------------------------------------------------
Very frequently it is useful to have a function perform actions which would benefit from a better knowledge of its calling environment. This would allow for more a more generic way of handling certain classes of problems. Hopefully the following example will demonstrate :

Very often, i would like to do something like the following :

function export_script_vars( $var_names ) {
   $result = "";
   foreach ($var_names as $var_name) {
      $result .= '<input type=hidden name="$var_name" value = "${$var_name}"/><br>'
   }
   return $result;
}

The intent of the script is to generally handle the export of a script's variables to hidden fields. The only problem, of course, is that the function has access only to its own symbol table, so to do something similar, we must resort to cut & past. This makes it impossible to have a generic function to perform this task. i can easily imagine other situations in which this occurs.

Proposal :
-----------
In TCL, there is the Uplevel syntax which allows a function to temporarily change its symbol scope to that of its parents within a program block.  This instructs the interpreter to search one level up for symbols unresolved in the current scope. The local symbol table would still be seached first (though im not sure if TCL handles it that way).

The syntax is similar to :

  uplevel {
    < Statements >
  }

or 

  uplevel (level) {
    < Statements >
  }

where level is the number of stack levels to "pop". Personally, the second option is not too important to me - the first would be fine.


With the proposed Uplevel syntax, the previous code would look like this ...

function export_script_vars( $var_names ) {
   $result = "";
   uplevel {
     foreach ($var_names as $var_name) {
        $result .= '<input type=hidden name="$var_name" value = "${$var_name}"/><br>'
     }
     return $result;
    }
}

With this addition, this opens great possibilities, though i can imagine the need to also be cautious.

Thanks for making PHP the great tool it is!

cc 
------------------------------------------------
"Fried Ice-Cream is a Reality!" - George Clinton

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 15:50 UTC] jani@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: Scripting Engine problem
 [2010-12-01 15:50 UTC] jani@php.net
Cryptic, not PHP way, etc. Outdated as well.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 13:01:30 2024 UTC