|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-06-10 12:16 UTC] kop at meme dot com
I want to get variables passed from the client into local (not global) variables. If the variable has not been passed from the client I do not want a local variable defined. While this can be coded for each variable I want to do this with, there is no way to abstract this operation into a function.
The (untested) code to get a form variable 'foo' would be:
if (array_key_exists($_POST, 'foo') {
$foo = &{$_POST['foo'])}
}
// $foo may or may not be defined, so if I've got strict
// error checking on I get a nice error if I ref an undefined
//variable. And, $foo is nice and local so it won't show
// where I don't send it.
If extract() took an array of keys as another argument, extracting only those keys that are in the list, I could get the form variables, foo, bar, and baz with:
extract($_POST, EXTR_REFS, '', array('foo', 'bar', 'baz'));
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 09:00:01 2025 UTC |
There is a glitch in my proposal, what if (in my example) there is no user supplied value for $foo, in that case if my program assigns a value to $foo, that value is local. But, if the user does supply a value for $foo, then $foo is a reference into $_POST, and the value can be seen elsewhere in the program. It's not a happy circumstance to have the scope of a value depend upon the presence or absence of some data in a data structure. It'd make for bugs. This could be solved with another kludge, like so: Instead of: extract($_POST, EXTR_REFS, '', array('foo', 'bar', 'baz')); do extract($_POST , EXTR_REF_INITS , '' , array('foo' => 'mydefaultval' , bar => 'mydefaultval' , 'baz' => UNASSIGNED)); UNASSIGNED is new. It is a php predefined constant containing the value you get if you reference a variable which has not had a value assigned to it. EXTR_REF_INITS is a new extract_type. It works like EXTR_REFS, but when the (new) array argument is present it will put key/value pairs into the first extract() argument iff the key does not already exist in the first argument. Clear like mud? I can't say for certain my proposal for extract() is the best solution. Any of it. It does seem a little of an aftermarket bolt-on. Not that there shouldn't be a solution to what I want to accomplish.