我为我的类编写了一个小的静态方法,$_POST
如果设置了变量,则返回变量NULL
。HTML 表单中的输入元素的名称带有连字符,例如“客户名称”。
所以我想我可以像这样访问它们$var = $_POST['customer-name']
。但是用我的方法:
public static function getPost($param) {
echo $param." = ".$_POST[$param]."<br/>";
return isset($_POST[$param]) ? $_POST[$param] : NULL;
}
我不能。echo
在我的方法中添加一些语句时,我注意到一些奇怪的行为。它会在连字符后切断所有内容,因此出现错误:
Notice: Undefined index: customer- in .. on line ..
这就是我测试它的方式:
$arr = (array)$object;
$newArr = array();
foreach($arr as $key => $val) {
$newKey = str_replace(get_class($object), "", $key);
$newArr[$newKey] = MyObject::getPost(strtolower(get_class($object))."-".$newKey);
}
这是我的测试的输出:
...
Notice: Undefined index: customer- in .. on line 116
customer-id =
Notice: Undefined index: customer- in .. on line 116
customer-name =
Notice: Undefined index: customer- in .. on line 116
customer-phonecode =
...
编辑 1 - 我被要求提供 HTML 表单:
<form action="" method="post" class="form-horizontal" role="form">
<input type="text" name="customer-name" id="customer-name" class="form-control" placeholder="Name" required="required" autocomplete="off" />
<select id="customer-phonecode" name="customer-phonecode" class="form-control">
<option value="+123"></option>
</select>
</form>
编辑 2 - 在phptester.net上测试 5.2、5.3、5.4、5.5 php 版本。得到同样的错误。
编辑 3 - 测试以下脚本。如果将字符串作为键传递,我会在超级全局 $_POST/an 数组中获得元素。但是如果传递指向字符串的变量,则无法访问元素
<?php
$test = array('customer-test1' => 1, 'customer-test2' => 2);
function getPost($param) {
global $test;
$newParam = (string)$param;
echo $param." = ".$test[$newParam]."<br/>";
return isset($test[$newParam]) ? $test[$newParam] : NULL;
}
class Customer {
private $test1;
private $test2;
function __construct() { }
}
$object = new Customer();
$arr = (array)$object;
$newArr = array();
foreach($arr as $key => $val) {
$newKey = str_replace(get_class($object), "", $key);
$newArr[$newKey] = getPost(strtolower(get_class($object))."-".$newKey);
}
这可能是一个 PHP 错误吗?