php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #24116 Enhancement: extract() takes array of keys
Submitted: 2003-06-10 12:16 UTC Modified: 2014-04-16 18:44 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: kop at meme dot com Assigned: levim (profile)
Status: Wont fix Package: *General Issues
PHP Version: * OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kop at meme dot com
New email:
PHP Version: OS:

 

 [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'));

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-10 21:37 UTC] 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.
 [2014-04-16 18:34 UTC] levim@php.net
-Status: Open +Status: Closed -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
 [2014-04-16 18:34 UTC] levim@php.net
I agree that `extract` is the wrong approach. If you have a better idea, feel free to reopen the bug.
 [2014-04-16 18:44 UTC] levim@php.net
-Status: Closed +Status: Wont fix
 [2014-04-16 18:44 UTC] levim@php.net
I guess 'Closed' is for implemented features or fixed bugs. It is unclear what the correct option for 'declined for now' would be, so I am using Won't Fix.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 14:01:37 2025 UTC