php - 使用 php OOP 检查空输入验证的类
问题描述
我不明白为什么这不起作用以及对 oop im 初学者关于 oop 范式的任何建议
我的课
class formvalidation {
public function check_empty_input($inputs=[]){
$checked = false;
foreach($inputs as $valor){
if(empty($valor)){
$checked = false;
break;
} else {
$checked = true;
}
}
if($checked = true){return true;}
else {return false;}
}
}
检查帖子是否为空
$formvalidation= new formvalidation();
$inputs = array($_POST['name'],$_POST['email'],$_POST['pass'],$_POST['confirmpass']);
if($formvalidation->check_empty_input($inputs))
解决方案
好吧,问题出在返回中,如果您使用=
的是比较运算符,而不是您应该使用的地方==
,因此该函数将始终返回 true ......您也应该从不使用的那一刻起使用静态函数需要一个对象来调用这个函数,试试这个:
<?php
class formvalidation {
public static function check_empty_input($inputs = []) : bool {
$everything_filled = true; //start with this supposition
foreach ($inputs as $valor) {
if (empty($valor)) {
$everything_filled = false; // is something is empty, than the supposition is false
break;
}
}
return $everything_filled; // return the supposition result value
}
}
$is_my_inputs_filled = formvalidation::check_empty_inputs([
$_POST['name'],
$_POST['email'],
$_POST['pass'],
$_POST['confirmpass'],
]);
如果它不起作用,请更好地解释“不起作用”的含义
推荐阅读
- azure-functions - CosmosDBTrigger 可靠地处理每个文档一次?
- javascript - 带有象限和线 x,y 的自定义 charts.js
- css - 如何通过打字稿更新 CSS
- python - 我无法为倒置图填充颜色,也无法自定义 y 轴。谁能帮我这个?
- mule - 如何从mule 4中的json中获取数组值
- html - 是否可以定位具有某些 CSS 样式的元素?
- azure - 移动租户但将 API / 应用程序保留在当前 Azure AD 中
- python - glob.glob() 返回空列表
- android - SwipeLayout - 无法解析 attr.xml 中的符号“drag_edge”
- angular - 日期管道:LOCALE_ID 不影响时区?