Page 1 of 1

Variable variable issue (GET and POST)

Posted: Tue Aug 08, 2006 4:50 am
by JayBird
Okay, say i have something like this

Code: Select all

class buildForm
{

    var $method;

    function buildForm($method)   // e.g. $method = "POST"
    {
        $this->method = $method;
    }

    function getValue($value)  // e.g. $value = "telNo"
    {
        echo $_POST[$value];
    }

}
So, you see in the above example, $method can be POST or GET.

Then in the function getValue() i want to echo a value that was submitted in a form, but it depend on the method of the form.

Basically, i need to know the correct way to do this

Pseudo:

Code: Select all

class buildForm
{

    var $method;

    function buildForm($method)   // e.g. $method = "POST"
    {
        $this->method = $method;
    }

    function getValue($value)  // e.g. $value = "telNo"
    {
        echo $_{$this->method}[$value];
    }

}

Posted: Tue Aug 08, 2006 4:58 am
by Benjamin
Could you just array_merge the $_GET & $_POST arrays?

Posted: Tue Aug 08, 2006 5:05 am
by JayBird
Yes, i suppose that could be an option.

I come up with this solution. Don't know if this is the best way or not though!?

Code: Select all

echo eval("return \$_$this->method[$value];");

Posted: Tue Aug 08, 2006 5:06 am
by Benjamin
feyd might slap you around for that one :wink:

Posted: Tue Aug 08, 2006 5:08 am
by JayBird
Image

Posted: Tue Aug 08, 2006 5:37 am
by s.dot
Would not a simple switch work? :P The possible values are 'get' or 'post'.

Code: Select all

function getValue($value)
{
   switch($this->method)
   {
      case 'get':
      echo $_GET[$value];
      break;

      case 'post':
      echo $_POST[$value];
      break;
   }
}

Posted: Tue Aug 08, 2006 5:48 am
by volka
You can also pass the http-parameters as array to either the constructor or a setter method.

Code: Select all

<?php
class buildForm
{
	var $method;
	var $params;
	
	function buildForm($method, $params)
	{
		$this->method = $method;
		$this->params = $params;
	}

	function setValue($name, $value) { /* ... */ }
	function mergeValues($newParams) { /* ... */ }
	
	function getValue($name)
	{
		if ( isset($this->params, $this->params[$name]) ) {
			echo $this->params[$name];
		}
		else {
			echo '--default--';
		}
	}
}

buildForm('POST', array_filter(array_map('trim',$_POST)));
?>
Maybe there's even no need for $method in the constructor

Code: Select all

function buildForm($params) {}
function printForm($method) {}

Posted: Tue Aug 08, 2006 6:06 am
by JayBird
@scottayy: yeah, had already though of that, was just seeing if there was a shorter way of doing it

@volka: looks like an interesting solution. Will try it out. Thanks

Posted: Tue Aug 08, 2006 8:56 am
by feyd
astions wrote:feyd might slap you around for that one :wink:
indeed. Image

Code: Select all

function getValue($value)  // e.g. $value = "telNo"
    {
        $var = '_' . $this->method;
        if (isset($$var) and array_key_exists($value, $$var))
        {
                return $$var[$value];
        }
        else
        {
                return false;
        }
    }

Posted: Tue Aug 08, 2006 10:18 am
by JayBird
Nice one feyd Image

Posted: Tue Aug 08, 2006 10:22 am
by feyd
for safety, it may be good to toss an is_array() call in there too.

Posted: Tue Aug 08, 2006 10:28 am
by JayBird
Actually feyd, will that work, becuase in the manual, it says this
PHP Manual wrote:Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.

Posted: Tue Aug 08, 2006 10:37 am
by feyd
You're right, it doesn't. But have no fear. $GLOBALS[$var] does work. :)