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 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效