简体   繁体   English

PHP $_GET/$_POST 通过变量变量

[英]PHP $_GET/$_POST via variable variables

I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables.我正在尝试使用变量变量动态访问 $_GET 和 $_POST 数组等。 The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific iterations for each.我尝试这样做的原因是我可以对多个数组执行类似的操作,而无需为每个数组声明特定的迭代。 I'm reasonably sure this is possible, as PHP's documentation says it is able to use variable variables to dynamically access an array, however I'm unable to get it to work.我有理由相信这是可能的,因为 PHP 的文档说它能够使用变量变量来动态访问数组,但是我无法让它工作。 A simple demonstration is when I'm attempting to verify that a certain property has been set.一个简单的演示是当我尝试验证某个属性是否已设置时。

if(isset(${$this->_array}[$property])) { return ${$this->_array}[$property]; }
else { return null; }

When I run the above script I always get null, however when I statically seek the desired property, manually using $_GET or $_POST, I get the desired outcome.当我运行上面的脚本时,我总是得到空值,但是当我静态地寻找所需的属性时,手动使用 $_GET 或 $_POST,我得到了所需的结果。 I have triple checked $this->_array and $property and they are returning the correct string values.我对$this->_array$property进行$this->_array三次检查,它们返回了正确的字符串值。 Are these arrays unavailable for such access, or am I doing something wrong?这些数组是否无法用于此类访问,还是我做错了什么?

Superglobals (such as $_POST ) can not be used as variable variables within functions .超全局变量(例如$_POST不能用作函数内的变量变量

You could say something like $post = $_POST;你可以这样说$post = $_POST; and then use 'post' and it'd work, but directly using '_POST' won't.然后使用'post'它会工作,但直接使用'_POST'不会。

Superglobals cannot be referenced as variable variables inside of classes or methods, so this will work:超全局变量不能作为类或方法内部的变量变量引用,因此这将起作用:

<?php
$var = "_GET";
print_r(${$var});

But this will not:但这不会:

<?php
test();
function test() {
  $var = "_GET";
  print_r(${$var});
}

I suspect that there is a better way to do what you are trying to accomplish.我怀疑有更好的方法来完成您想要完成的工作。

http://php.net/manual/en/language.variables.superglobals.php#refsect1-language.variables.superglobals-notes http://php.net/manual/en/language.variables.superglobals.php#refsect1-language.variables.superglobals-notes

Whatever you're doing wrong, using variable variables is probably making it worse.无论您做错了什么,使用可变变量可能会使情况变得更糟。 For your own sanity, please stop.为了你自己的理智,请停下来。 They should never be deployed in production code under any circumstances.在任何情况下都不应该在生产代码中部署它们。 They're impossible to debug, and using them in your code is like trying to read something that someone else wrote with their feet.它们无法调试,在你的代码中使用它们就像试图阅读别人用脚写的东西。 If they have particularly dexterous feet, then perhaps you can understand what they're doing.如果他们的脚特别灵巧,那么也许你能理解他们在做什么。 But 99.9999% of the time, it is better to just use normal arrays.但是在 99.9999% 的情况下,最好只使用普通数组。

That being said, try $_REQUEST instead.话虽如此,请改为尝试$_REQUEST

There's already an array that contains both $_GET and $_POST .已经有一个包含$_GET$_POST的数组。 It's named $_REQUEST .它被命名为$_REQUEST Having said that, it can also contain the contents of $_COOKIE depending on the request_order setting, but the default is just $_GET and $_POST .话虽如此,它也可以根据request_order设置包含$_COOKIE的内容,但默认值只是$_GET$_POST

you can do this but dont know if it is a good coding practice你可以这样做,但不知道这是否是一个好的编码习惯

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
     $method = '_POST';
 }
 else {
    $method = '_GET';
 }
 $data = $$method;

You can create an associative array that references both arrays, and use that.您可以创建一个引用两个数组的关联数组,然后使用它。

$params = [
    '_GET' => $_GET,
    '_POST' => $_POST
];

Then you can use然后你可以使用

return $params[$this->_array][$property] ?? null;

You say you want to access both the $_GET and $_POST arrays, among others -- what are these 'others'?你说你想同时访问both the $_GET and $_POST arrays, among others ——这些“其他”是什么? You can use $_REQUEST to check the contents of $_GET , $_POST , and $_COOKIE all at once.您可以使用$_REQUEST一次性检查$_GET$_POST$_COOKIE的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM