ID: 24116
User updated by: kop at meme dot com
Reported By: kop at meme dot com
Status: Open
Bug Type: Feature/Change Request
Operating System: All
PHP Version: 4.3.2
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2003-06-10 12:16:47] 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'));
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=24116&edit=1