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
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Aug 08, 2006 4:50 am
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];
}
}
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Aug 08, 2006 4:58 am
Could you just array_merge the $_GET & $_POST arrays?
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Aug 08, 2006 5:05 am
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];");
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Aug 08, 2006 5:06 am
feyd might slap you around for that one
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Aug 08, 2006 5:08 am
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Tue Aug 08, 2006 5:37 am
Would not a simple switch work?
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.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Aug 08, 2006 5:48 am
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) {}
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Aug 08, 2006 6:06 am
@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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 08, 2006 8:56 am
astions wrote: feyd might slap you around for that one
indeed.
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;
}
}
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Aug 08, 2006 10:18 am
Nice one feyd
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 08, 2006 10:22 am
for safety, it may be good to toss an is_array() call in there too.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Aug 08, 2006 10:28 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 08, 2006 10:37 am
You're right, it doesn't. But have no fear. $GLOBALS[$var] does work.