Req #26545 [Com]: Cannot extract() variables into class scope
| From: | narf at devilix dot net | Date: | Wed, 05 Mar 2014 20:22:43 +0000 |
| Subject: | Req #26545 [Com]: Cannot extract() variables into class scope | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-184536@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=26545&edit=1
ID: 26545
Comment by: narf at devilix dot net
Reported by: kmsluder at optonline dot net
Summary: Cannot extract() variables into class scope
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: Linux 2.4.2
PHP Version: 4.3.4
Block user comment: N
Private report: N
New Comment:
This looks like a duplicate of #10564
https://bugs.php.net/bug.php?id=10546
P.S.: The referenced pull request has been closed.
Previous Comments:
------------------------------------------------------------------------
[2009-02-27 08:07:44] james dot relyea at zifiniti dot com
Adding in ability to specifiy scope would be highly useful for Objects.
------------------------------------------------------------------------
[2004-01-10 10:41:10] andrey@php.net
What's the deal to use extract() to add dynamically member variables to the object?
------------------------------------------------------------------------
[2003-12-06 16:53:27] derick@php.net
It's still not a bug as extract() was never meant to do this. Making it a feature-request.
------------------------------------------------------------------------
[2003-12-06 16:20:21] 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:11:31] 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
}
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=26545&edit=1