douqilai4263 2016-06-27 14:57
浏览 14
已采纳

PHP类中的错误处理

I still playing with PHP and OOP. But not understand how to pull back errors from the class.

index file

include 'class.php';
$test = new magic('', '', '33');
$test->getfullname();
foreach ($test->get_errors() as $error) {
    echo $error . '<br>';
}

class:

class magic
{

    private $name;
    private $surname;
    private $age;
    private $errors = array();

    function __construct($name, $surname, $age)
    {
        $this->name = $name;
        $this->surname = $surname;
        $this->age = $age;
    }

    public function get_errors()
    {
        return $this->errors;
    }

    public function getname()
    {
        if (!empty($this->name)) {
            return true;
        } else {

            array_push($this->errors, 'Please check name');
            return false;
        }
    }

    public function getsurname()
    {
        if (!empty($this->surname)) {
            return true;
        } else {

            array_push($this->errors, 'Please check surname');
            return false;
        }
    }

    public function getfullname()
    {
        if (($this->getname()) && ($this->getsurname())) {
            echo $this->name . ' ' . $this->surname;
        }
    }

}

My question is why when name or surname is empty then returning please check name or surname but when both are empty then return only first? How to candle these type errors in PHP class and what is best practice to do that? I don't think i can use try/catch exceptions in this scenario.

  • 写回答

2条回答 默认 最新

  • duandan1995 2016-06-27 16:25
    关注

    I suggest handling errors in the constructor and throwing exception.

    class magic
    {
    
        /**
         * @param string $name
         * @param string $surname
         * @param int $age
         * @throws Exception
         */
        public function __construct($name, $surname, $age)
        {
            $errors = [];
    
            if (empty($name)) {
                $errors[] = 'Name is required.';
            }
    
            if (empty($surname)) {
                $errors[] = 'Surname is required.';
            }
    
            if (!empty($errors)) {
                throw new Exception(implode('<br />', $errors));
            }
    
            $this->name = $name;
            $this->surname = $surname;
            $this->age = $age;
        }
    
        public function printFullname()
        {
            echo $this->name . ' ' . $this->surname;
        }
    
    }
    

    client:

    include 'class.php';
    
    try {
        $test = new magic('', '', '33');
        $test->printFullname();
    } catch (Exception $exc) {
        echo $exc->getMessage(); //error messages
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀