Destramic Posted May 2, 2010 Share Posted May 2, 2010 i'm having a problem getting this to work... $this->method = "POST" if anyone could help please $field_value = $_{$this->Method}[$field_name]; Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2010 Share Posted May 2, 2010 The $_POST array is a perfectly usable array variable. By making a series of named variables from it, you must now keep track of each of those variable names and write out code to reference each one (or use more variable variables to access them.) That does not make for general purpose coding (using an OOP class) or efficient coding. Just use the elements in the $_POST array directly without going through an additional intermediate step of creating named variables (this will save processing time and memory as well.) Variable variables take three times longer to access than accessing an array variable. Are you sure you want to use variable variables? Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1051893 Share on other sites More sharing options...
Destramic Posted May 2, 2010 Author Share Posted May 2, 2010 as the method can change from _POST - _GET variable varable is what i need for this certian code...do you know why the code above isn't working please? Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1051896 Share on other sites More sharing options...
Alex Posted May 2, 2010 Share Posted May 2, 2010 I'm not sure variable variables are the way to go in this case, but the code you're looking for is: $field_value = ${'_' . $this->Method}[$field_name]; Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1051955 Share on other sites More sharing options...
Destramic Posted May 2, 2010 Author Share Posted May 2, 2010 i tried the code and it returned no value <?php echo $field_value = ${"_" . $this->Method}[$field_name]; // but both vars below returned values echo $_POST[$field_name]; echo $this->Method; ?> so the code you gave me didn't seem to have worked Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1051980 Share on other sites More sharing options...
Alex Posted May 2, 2010 Share Posted May 2, 2010 Does $this->Method contain 'POST'? Because that should work. $_POST['var'] = 'val'; $method = 'POST'; $key = 'var'; echo $var = ${'_' . $method}[$key]; Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1051982 Share on other sites More sharing options...
Destramic Posted May 2, 2010 Author Share Posted May 2, 2010 yep POST is the value set for the method...but has no return here is my code...but i am running this script off my apache server...possible some setting are wrong in my php.ini file? <?php class Form_Validation { public $Form_Name; public $Fields = array(); public $Method = "POST"; public $Errors = false; public function __construct($form_name) { $this->Form_Name = $form_name; } public function Validate_Field($field_name, $field_type, $error_message) { $this->Fields[$field_name]['name'] = $field_name; $this->Fields[$field_name]['type'] = $field_type; $this->Fields[$field_name]['error_message'] = $error_message; } public function Display_Errors() { foreach ($this->Fields as $field) { $field_name = $field['name']; $field_type = $field['type']; $error_message = $field['error_message']; echo $field_value = ${"_" . $this->Method}[$field_name]; echo $_POST[$field_name]; echo $this->Method; } } public function Validate_Form() { } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1051986 Share on other sites More sharing options...
Alex Posted May 2, 2010 Share Posted May 2, 2010 So... $field_value = ${"_" . $this->Method}[$field_name]; echo $field_value; // Nothing? echo $_POST[$field_name]; // This works though? Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052000 Share on other sites More sharing options...
Destramic Posted May 2, 2010 Author Share Posted May 2, 2010 it all works but this part doesn't $field_value = ${"_" . $this->Method}[$field_name]; Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052004 Share on other sites More sharing options...
Zane Posted May 2, 2010 Share Posted May 2, 2010 $x = "_" . $this->Method; $field_value = ${$x}[$field_name]; If that doesn't work, then somethings up. Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052090 Share on other sites More sharing options...
Destramic Posted May 2, 2010 Author Share Posted May 2, 2010 this is strange...its not working...maybe something to do with my setting with php/apache? which may be stopping it from working? Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052138 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2010 Share Posted May 2, 2010 There are no php.ini settings that directly affect what you are trying. Perhaps if you posted enough of your code that is producing the $_POST data and using the class so that someone could duplicate the problem? Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052139 Share on other sites More sharing options...
Destramic Posted May 3, 2010 Author Share Posted May 3, 2010 heres the two pages if you can help please Form_Validation.php <?php class Form_Validation { public $Form_Name; public $Fields = array(); public $Method = "POST"; public $Errors = false; public function __construct($form_name) { $this->Form_Name = $form_name; } public function Validate_Field($field_name, $field_type, $error_message) { $this->Fields[$field_name]['name'] = $field_name; $this->Fields[$field_name]['type'] = $field_type; $this->Fields[$field_name]['error_message'] = $error_message; } public function Display_Errors() { foreach ($this->Fields as $field) { $field_name = $field['name']; $field_type = $field['type']; $error_message = $field['error_message']; echo $field_value = ${"_" . $this->Method}[$field_name]; } } public function Validate_Form() { } } ?> Form.php <?php require_once $_SERVER['DOCUMENT_ROOT'] . "Form_Validation.php"; $Form = new Form_Validation("test"); $Form->Validate_Field("name", "text", "Please "); $Form->Display_Errors(); echo <<<HTML <form action="{$_SERVER['REQUEST_URI']}" name="test" method="POST"> <fieldset> <legend>Add League</legend> <label for="name">League Name:</label> <input type="text" name="name" title="League Name"/><br /> <input type="submit" value="Add League" title="Submit Form" /> <input type="reset" value="Reset" title="Reset Form" /> </fieldset> </form> HTML; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052157 Share on other sites More sharing options...
Alex Posted May 3, 2010 Share Posted May 3, 2010 Doh, I should have realized, I remember someone else encountering a similar problem like this a while back. The solution? From the manual on variable variables. Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. http://php.net/manual/en/language.variables.variable.php Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052159 Share on other sites More sharing options...
Destramic Posted May 3, 2010 Author Share Posted May 3, 2010 well thank you anyways so to have wasted your time...back to the drawing board Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052163 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 LOL, I was just about to post the same thing (if you test your code with full error_reporting/display_errors it will give you a clue about what is happening because the variable variable that was created "_POST", does not exist.) You would want to pass ANY data that the class operates on into the class as a parameter anyway to make the code general purpose, which will solve the $_POST problem at the same time (untested.) edit: (just tested.) Quote Link to comment https://forums.phpfreaks.com/topic/200445-variable-variables/#findComment-1052167 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.