Variable variable issue (GET and POST)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Variable variable issue (GET and POST)

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

}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Could you just array_merge the $_GET & $_POST arrays?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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];");
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

feyd might slap you around for that one :wink:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Image
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
   }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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) {}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
        }
    }
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Nice one feyd Image
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for safety, it may be good to toss an is_array() call in there too.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're right, it doesn't. But have no fear. $GLOBALS[$var] does work. :)
Post Reply