MrFish Posted June 5, 2013 Share Posted June 5, 2013 Is it possible to recreate php's extract function? I have a custom implementation I'd like to write. I really just need the change the scope of variables created in a function the without using the GLOBALS array. Link to comment Share on other sites More sharing options...
jeffman Posted June 5, 2013 Share Posted June 5, 2013 I can't comment on the wisdom of this, since I don't really understand your explanation, and it probably doesn't matter. The trick to doing what you want is to use variable variables. This simple loop does the basic extraction. To get the utility provided by all the constants that extract() can use, you'll have to add more code. foreach ($var_array as $key => $value) { $$key = $value;} Link to comment Share on other sites More sharing options...
MrFish Posted June 5, 2013 Author Share Posted June 5, 2013 (edited) Well I was trying to get around using this foreach loop everytime I called the function and was hoping I could write a function like the extract function. http://php.net/manua...ion.extract.php But I couldn't think of any way to match the functionality exactly. $arr = array("test"=>1,"test2"=>2,"test3"=>3); function extract2($arr){ foreach($arr as $key => $val) { $$key = $val; }} function extract3($arr){ foreach($arr as $key => $val) { $GLOBALS[$key] = $val; }} extract2($arr);echo $test; // obviously this wouldn't work because the scope of the variables are in the function extract3($arr);echo $GLOBALS["test"]; // works- yes- but not what I intended If that makes sense. EDIT: How extract works now- $arr = array("test"=>1,"test2"=>2,"test3"=>3);extract($arr); echo $test; // prints 1 Edited June 5, 2013 by MrFish Link to comment Share on other sites More sharing options...
justsomeguy Posted June 5, 2013 Share Posted June 5, 2013 If you want the variables exported into the symbol table of the current scope, then why wouldn't you just use extract? What is the reason that you don't want to use extract? Link to comment Share on other sites More sharing options...
MrFish Posted June 5, 2013 Author Share Posted June 5, 2013 (edited) Because I have a lot of xml to parse and to make it simpler I wanted to create a function that would run through an element recursively and create variables without any other hassle. $valueSheet = <<<TXTcom_status=[Status]com_priceLow=[PriceLow]com_priceHigh=[PriceHigh]com_sqftLow=[SqftLow]com_sqftHigh=[SqftHigh]com_number=SubdivisionNumbercom_name=SubdivisionNamecom_description=SubdivisionDescriptioncom_street1=SubAddress->SubStreet1com_city=SubAddress->Citycom_state=SubAddress->Statecom_lat=SubAddress->SubGeocode->SubLatitudecom_lng=SubAddress->SubGeocode->SubLongitudecom_directions=DrivingDirectionsTXT; getValues($Subdivision, $valueSheet); echo $com_name; // Some Community The best I can do is return an array and use extract on that. It's not a road block and 9 extra characters won't kill me I just wanted to see if I could do it like extract is doing it. But I'm guessing I can't replicate that functionality in php. Edited June 5, 2013 by MrFish Link to comment Share on other sites More sharing options...
justsomeguy Posted June 5, 2013 Share Posted June 5, 2013 I don't think there's a way to make a variable in a specific scope other than the current one or globally, but you can use a variable prefix or on the manual page it has examples for extracting to properties of an object or elements of an array if you want to group everything. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now