首先,有个函数是这么写的。
function exam(){
//从数据库获取数据。
$records = $aModel->where(['type' => 1])->get();
if(empty($records)) return false;
$newRecord = $bModel->map();
foreach($records as $item){
$newRecord->name = $item->name;
$newRecord->age = $item->age;
try{
$bModel->insert($newRecord);
}
catch(\Exception $e){
\Log::catch($e);
}
}
return true;
}
然后,这个函数是这么调用的。
if(true !== exam()){
echo "表更新失败";
return false;
}
请各位经验丰富的phper看下,这其中有几个地方看着挺别扭的:
函数exam()中,return 的位置是否合适。
假如遇到函数中返回类型不确定,执行完成返回结果,执行失败返回false这种情况下,调用的时候是否适合对结果进行类似 if(false === exam()) 的判断。
感觉在if判断中执行函数,会使得代码的可读性降低,不知是否存在这样的问题。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
1.函数exam()中,return 的位置是否合适。
2.假如遇到函数中返回类型不确定,执行完成返回结果,执行失败返回false这种情况下,调用的时候是否适合对结果进行类似 if(false === exam()) 的判断。
3.感觉在if判断中执行函数,会使得代码的可读性降低,不知是否存在这样的问题。
你的try..catch()...不写return false,
意思是插入失败也会返回true