Domcsore Posted April 17, 2010 Share Posted April 17, 2010 I currently have this: $engineSuffix = '.global_conf.php'; $objSuffix = '_conf'; $engine[0] = 'template'; $engine[1] = 'admin'; $engineCount = count($engine); $e = 0; while ($e<$engineCount){ include_once($engine[$e].$engineSuffix); $e++; } but I would like something like this: $engineSuffix = '.global_conf.php'; $objSuffix = '_conf'; $engine[0] = 'template'; $engine[1] = 'admin'; $engineCount = count($engine); $e = 0; while ($e<$engineCount){ include_once($engine[$e].$engineSuffix); $e++; $'VALUE OF ARRAY $engine[$e] AND VALUE OF $objSuffix AS VARIABLE NAME' = new $engine[$e]; } Is there a way to do this? Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/ Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 I see what you mean. Assign the result of the array to a variable. We'll use a string for exaple $myvariable = 'Array'; $$variable2 = 'array2'; $$variable, is infact $Array, and holds the value "array2" Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043794 Share on other sites More sharing options...
Domcsore Posted April 17, 2010 Author Share Posted April 17, 2010 Exactly what I need cheers mate. Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043803 Share on other sites More sharing options...
Domcsore Posted April 17, 2010 Author Share Posted April 17, 2010 Hmn I wrote up a test script and tried inserting: ${$engine[$e].$objSuffix} = new $engine[$e]; into the while loop but failed to work? Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043811 Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 Hmn I wrote up a test script and tried inserting: ${$engine[$e].$objSuffix} = new $engine[$e]; into the while loop but failed to work? I'm not sure if its the same thing.. but try.. $var = $engine[$e].$objSuffix; $$var = new $engine[$e]; Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043818 Share on other sites More sharing options...
Domcsore Posted April 17, 2010 Author Share Posted April 17, 2010 An error again... I think it does not recognise the engine variable as a class so does not like it, however, I get an error saying maximum file size exceeded which seems as if the loop keeps repeating over and over? Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043825 Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 $var = $engine[$e].$objSuffix; echo $var; Does this produce a different value until the while loop ends? Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043827 Share on other sites More sharing options...
Domcsore Posted April 17, 2010 Author Share Posted April 17, 2010 Sorry for late reply, but yes if I just run that it displays admin_conf and template_conf as it should. ($engine[0] and [1] are 'admin' and 'template' and $objSuffix is '_conf') Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043938 Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 What exactly gets echo'd. If there is spaces inside the string.. then you could try.. ${$var} = new $engine[$e]; It might be illegal characters for variable names. Why do you want to asign it to a variable anyway.. there might be another solution? Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043941 Share on other sites More sharing options...
Domcsore Posted April 17, 2010 Author Share Posted April 17, 2010 Basically what I am trying to do is load extend classes from a separate file, however I just want the files to include and create a new object automatically from the array name given. For example my template class would be like so: file name: template.global_conf.php that would be a template class which is an extend of my global_conf class. so in my global_conf class __construct function I have it so it includes the files automatically as shown above however I have issues trying to create the template object in the loop. (Should have really explained my objective to start, sorry) Link to comment https://forums.phpfreaks.com/topic/198855-create-a-variable-from-array-value/#findComment-1043950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.