$_POST
設定されているかどうかにかかわらず、変数を返すクラス用の小さな静的メソッドを作成しましたNULL
。HTML フォームの入力要素には、'customer-name' などのハイフンを含む名前があります。
だから、このようにアクセスできたと思います$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 =
...
EDIT 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>
EDIT 2 - 5.2、5.3、5.4、5.5 php バージョンのphptester.netでテスト済み。同じエラーが発生します。
EDIT 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のバグでしょうか?