Edit report at https://bugs.php.net/bug.php?id=24116&edit=1
ID: 24116
Updated by: levim@php.net
Reported by: kop at meme dot com
Summary: Enhancement: extract() takes array of keys
-Status: Open
+Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
-Operating System: All
+Operating System: Any
-PHP Version: 4.3.2
+PHP Version: *
-Assigned To:
+Assigned To: levim
Block user comment: N
Private report: N
New Comment:
I agree that extract is the wrong approach. If you have a better idea, feel free to
reopen the bug.
Previous Comments:
------------------------------------------------------------------------
[2003-06-10 21:37:51] kop at meme dot com
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.
------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=24116&edit=1