#26545 [Com]: Cannot extract() variables into class scope
| From: | james dot relyea at zifiniti dot com | Date: | Fri, 27 Feb 2009 08:07:45 +0000 |
| Subject: | #26545 [Com]: Cannot extract() variables into class scope | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-134243@lists.php.net to get a copy of this message | ||
ID: 26545
Comment by: james dot relyea at zifiniti dot com
Reported By: kmsluder at optonline dot net
Status: Open
Bug Type: Feature/Change Request
Operating System: Linux 2.4.2
PHP Version: 4.3.4
New Comment:
Adding in ability to specifiy scope would be highly useful for Objects.
Previous Comments:
------------------------------------------------------------------------
[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 http://bugs.php.net/?id=26545&edit=1