[英]php variable array name
我得到了以下课程,我想知道是否可以使用变量数组名称。
class Ajax{
private $method;
public function __construct(){
$this->method = '$_' . $_SERVER['REQUEST_METHOD'];
}
}
因此,基本上$ method变量应该包含POST或GET方法,下一个问题也是在此处使用引用是否明智?
我的第一个想法是:
$this->method = '$_' . $_SERVER['REQUEST_METHOD'];
$this->metod =& $$this->method;
但这是行不通的。
感谢您的阅读和帮助,非常感谢。
你想要类似的东西
$this->method = $$_SERVER['REQUEST_METHOD'];
这是一个“ 变量变量 ”(请注意双$$)。 但是,请不要这样做。 变量变量导致难以调试的代码。
为什么不做
If ($_SERVER['REQUEST_METHOD'] === 'GET') {
$this->method = $_GET;
} else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$this->method = $_POST;
}
如果您想直接工作,那么
$this->method = ${'_'.$_SERVER['REQUEST_METHOD']};
或者您可以只使用$_REQUEST
(尽管使用它不是很好)
$this->method = $_REQUEST;
尝试这样获得它:
$this->method = ${'_' . $_SERVER['REQUEST_METHOD']};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.