我尝试验证PHP中的选择字段。我编写的代码,但解释器返回我做出以下声明:未定义的属性,如果值为空
公告:未定义的属性:TEST5 :: $财产 /opt/lampp/htdocs/mvc3/controller/admin/test5.php第22行NULL
当我没有从列表中选择任何东西时会出现问题。有谁知道如何解决这一问题?
我提出代码:
<form action="test5.php" method="post">
<select name="select">
<option selected="selected" disabled="disabled">Select...</option>
<option>Apple</option>
<option>Raspberry</option>
<option>Banana</option>
<option>Pineaple</option>
</select>
<button type="submit" name="button">Send</button>
</form>
<?php
class Test5 {
private $paramName, $default;
public function getParam($paramName, $default = null) {
if (isset($_POST[$paramName])) {
$this->property = trim($_POST[$paramName]);
}
return $this;
}
public function getProperty() {
return $this->property;
}
}
$test5 = new Test5;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$test5->getParam('select', false);
var_dump($test5->getProperty());
}
?>
我没仔细看过,但这尖叫我的脸'$ default = null' – Andreas
尝试添加$ property的私有属性'private $ paramName,$ default,$ property;' –