php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #26545 Cannot extract() variables into class scope
Submitted: 2003-12-06 16:11 UTC Modified: 2014-08-19 16:11 UTC
Votes:6
Avg. Score:3.3 ± 1.7
Reproduced:4 of 6 (66.7%)
Same Version:1 (25.0%)
Same OS:3 (75.0%)
From: kmsluder at optonline dot net Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 4.3.4 OS: Linux 2.4.2
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: kmsluder at optonline dot net
New email:
PHP Version: OS:

 

 [2003-12-06 16:11 UTC] kmsluder at optonline dot net
Description:
------------
extract() cannot be used inside an object member function to set object var's.  Using extract normally brings the variables into the scope of the function (as expected), but there is no way to bring array key/value pairs to object scope without a foreach statement.

It would be nice to specify what scope the variables should be created in, or even create a different function specifically to set the member variables of an object.

Reproduce code:
---------------
class MyClass
{
    var $m1;  // Imagine a class with fifty variables,
    var $m2;  // Not all of which must be set.
    var $m3;  // This is why you would want to use extract().

    function Init($info)
    {
        extract($info, EXTR_OVERWRITE); // Doesn't work; extracted to function scope

        extract($info, EXTR_PREFIX_ALL, "$this->"); // Stupid idea; not a valid variable name

        foreach($info as $key => $value)
        {
            $this->$key = $value;  // Works, but ugly
        }
    }
}


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-06 16:20 UTC] kmsluder at optonline dot net
Note to anyone that says that the extra two lines aren't a big deal: it's a lot more than two if you need to emulate some of the more sophisticated (and rather useful) features of extract().
 [2003-12-06 16:53 UTC] derick@php.net
It's still not a bug as extract() was never meant to do this. Making it a feature-request.
 [2004-01-10 10:41 UTC] andrey@php.net
What's the deal to use extract() to add dynamically member variables to the object?
 [2009-02-27 08:07 UTC] james dot relyea at zifiniti dot com
Adding in ability to specifiy scope would be highly useful for Objects.
 [2014-03-05 20:22 UTC] narf at devilix dot net
This looks like a duplicate of #10564

https://bugs.php.net/bug.php?id=10546

P.S.: The referenced pull request has been closed.
 [2014-08-19 16:11 UTC] levim@php.net
-Status: Open +Status: Duplicate -Package: Feature/Change Request +Package: *General Issues
 [2014-08-19 16:11 UTC] levim@php.net
Closing this as a duplicate of bug 10546.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC