Jump to content

variable variables


Destramic

Recommended Posts

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?

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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()
{

}
}
?>

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

Doh, I should have realized, I remember someone else encountering a similar problem like this a while back. The solution?  :rtfm:

 

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

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.