flaab Posted October 31, 2007 Share Posted October 31, 2007 Hi I've a little problem. We can iterate an array or a hash doing something right? Well...what I need is something similar but with all the defined variables within my Scope. A little pseudocode example.. class foo extends whatever { public function doSome() { /* lots of variable declaration and assignation */ foreach(Declared_variable_in_my_scope as $name => $value) do something with it } } } Is there any way to do this? Thx! Quote Link to comment https://forums.phpfreaks.com/topic/75471-foreach-exiting-variable-in-a-blockdo/ Share on other sites More sharing options...
bache Posted October 31, 2007 Share Posted October 31, 2007 You can put your variables in array and loop in this array. Quote Link to comment https://forums.phpfreaks.com/topic/75471-foreach-exiting-variable-in-a-blockdo/#findComment-381824 Share on other sites More sharing options...
trq Posted October 31, 2007 Share Posted October 31, 2007 <?php foreach(get_defined_vars() as $var) { // ${$var} will now hold you variable. } ?> Be aware that get_defined_vars() will only locate the variables in the same scope that it is called from. Quote Link to comment https://forums.phpfreaks.com/topic/75471-foreach-exiting-variable-in-a-blockdo/#findComment-381861 Share on other sites More sharing options...
flaab Posted October 31, 2007 Author Share Posted October 31, 2007 Thanks!!!!!!!! :-) Quote Link to comment https://forums.phpfreaks.com/topic/75471-foreach-exiting-variable-in-a-blockdo/#findComment-382212 Share on other sites More sharing options...
flaab Posted November 1, 2007 Author Share Posted November 1, 2007 I again! That works but I need to make that class function, also available for the subclasses. Is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/75471-foreach-exiting-variable-in-a-blockdo/#findComment-382792 Share on other sites More sharing options...
trq Posted November 1, 2007 Share Posted November 1, 2007 As I said, get_declared_vars() will locate all variables within the same scope. Is that your problem? Quote Link to comment https://forums.phpfreaks.com/topic/75471-foreach-exiting-variable-in-a-blockdo/#findComment-382848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.