如何在php中显示方法的验证错误


how to display validation errors from methods in php

我正在编写一个类,从数据库下载一个包含数据的csv文件
我使用的方法如下
1.调用其他方法的主方法
例如:

public function main(){
    $this->a();
    $this->b();
    $this->c();
}

假设方法a是验证,b是查询执行,c是文件下载。这三种方法都有可能产生错误,例如:查询执行错误、下载错误等

当我在这些方法中出现错误时,我必须返回并显示错误。处理这个问题的最佳方法是什么?

如果给每个方法一个返回值,则如果该方法没有错误,则可以返回零;如果发生错误,则返回表示错误的非零值。让您的主函数检查每个方法的结果,看看是否发生了任何错误,如果发生了,则显示它们。用于捕获返回值(指示您是否有错误)的代码可能看起来像:

public function main(){
    if ($this->a() != 0) {
        // Display the error represented by the return code
    }
    if ($this->b() != 0) {
        // Display the error represented by the return code
    }        
    if ($this->c() != 0) {
        // Display the error represented by the return code
    }