views:

454

answers:

6

Hi, I'm building a small abstract class that's supposed to make certain tasks easier.

For example: $var = class::get('id'); would run check if there's pointer id in the $_GET, returning a string or array according to parameters. This should also work for post and request and maby more.

I'm doing it in the way there's function for all the superglobals. I'm using get as example:

get function gets a pointer as parameter, it calls fetchdata function and uses the pointer and "$_GET" as the parameters.

fetchdata is supposed to just blindly use the string it got as superglobal and point to it with the other param. Then check if it exists there and return either the value or false to get function, that returns the value/false to caller.

Only problem is to get the string work as superglobal when you don't know what it is. I did this before with a switch that checked the param and in case it was "get", it set $_GET to value of another variable. However I don't want to do it like that, I want it to be easy to add more functions without having to touch the fetchdata.

I tried $method = eval($method), but it didn't work. ($method = "$_GET"), any suggestions?

EDIT: Sorry if I didn't put it clear enough. I have a variable X with string value "$_GET", how can I make it so X gets values from the source described in the string?

So simply it's

$X = $_GET if X has value "$_GET"

$X = $_POST if X has value "$_POST"

I just don't know what value X has, but it needs to get data from superglobal with the same name than its value.

A: 

Show us code.

orlandu63
A: 

You can use $_REQUEST["var"] instead of $_GET["var"] or $_POST["var"].

A more complicated way would be to test if the variable exists in the GET array, if it doesnt then its POST. If it does its GET.

$var = null;
if (isset($_GET["varname"]))
{
  $var = $_GET["varname"];
}
else
{
  $var = $_POST["varname"];
}
Gushiken
A: 

If you want a variable to be accessible globally, you can add it tot he $GLOBALS array.

$GLOBALS['test']='test';

Now you can fetch $GLOBALS['test'] anywhere.

Click Upvote
+1  A: 

I'm not quite sure what you're trying to achieve, but you could have a look at the __callStatic magic method

class example{
 protected static $supers = array('GET', 'POST', 'SERVER', 'COOKIE');
 public static function __callStatic($functionName, $arguments){
  $index = arguments[0];
  $desiredSuper = strtoupper($functionName);
  if(in_array($desiredSuper, self::$supers)){
   doStuff ( $_{$desiredSuper}[$index] );
  }
  else{
    throw new Exception("$desiredSupper is not an allowed superGlobal");
  }
 }
}

you could then do:

 example::get('id'); //wo do stuff to $_GET['id']
 example::server('REQUEST_METHOD'); //Will do stuff to $_SERVER['REQUEST_METHOD']
 example::foo('bar'); //throws Exception: 'FOO is not an allowed superGlobal'

Php manual on magic methods: http://ca.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods

Edit
I just noticed your edit, you could try:

 $X  = {$X};
Pim Jager
+1  A: 

According to this page in the manual:

Note: Variable variables

Superglobals cannot be used as variable variables inside functions or class methods.

This means you can't do this inside a function or method (which you would be able to do with other variables) :

$var = '_GET';
${$var}[$key]

Instead of passing a string to fetchdata(), could you not pass $_GET itself? I think PHP will not copy a variable unless you modify it ('copy on write'), so this shouldn't use memory unnecessarily.

Otherwise there are only nine superglobals, so a switch-case as you have suggested isn't unreasonable.

You could do this with eval() if you really had to, something like:

eval('return $_GET;');

I think that would be unnecessary and a bad idea though; it is slow and you need to be extremely careful about letting untrusted strings anywhere near it.

Tom Haigh
+2  A: 

Don't use eval. Just use reference.

//test value for cli
$_GET['test'] = 'test';

/**
 * @link http://php.net/manual/en/filter.constants.php reuse the filter constants
 */
function superglobalValue($key, $input = null) {
    if ($input === INPUT_POST)
     $X = &$_POST;
    else
     $X = &$_GET;
    return (isset($X[$key]) ? $X[$key] : false); 
}

function getArrayValue(&$array, $key) {
    return (array_key_exists($key, $array) ? $array[$key] : false); 
}

//test dump
var_dump(
    superglobalValue('test', INPUT_GET),
    superglobalValue('test', INPUT_POST),
    getArrayValue($_GET, 'test'),
    getArrayValue($_POST, 'test')
);

$_GET, $_POST and $_REQUEST dont have any null values by default, only string or array. So I used isset there instead of array_key_exists.

Param order: I always put required params before optional when I can, and the data objects before the manipulation/subjective params. Thats why key is first param for superglobalValue and second param for getArrayValue.

OIS