【问题标题】:Exception handling in oop PHP not workingoop PHP中的异常处理不起作用
【发布时间】:2014-10-19 17:21:21
【问题描述】:

我对面向对象的 PHP 非常陌生,并尝试了一些基本示例来掌握 oop php。我上面有一个简单的例子,我正在尝试学习异常处理并在年龄大于 20 但不工作时生成异常错误消息。

<?php
interface read_methods 
{
    public function read_age($age);
}
abstract class  person 
{ 
    var $gender;
    var $animal;
    var $birds;
    abstract function group($group);
    function people($value)
    {
        $this->gender=$value;
    }
    function animals($value)
    {
        $this->animal=$value;
    }
    function bird($value)
    {
        $this->birds=$value;
    }
}

class behaviour extends person implements read_methods
{
    var $nonhuman;
    function get_all()
    {
        return $this->people();
        return $this->animals();
        return $this->bird();
    }
    function non_human($nonhuman)
    {
        return $this->alien=$nonhuman;
    }
    function read_age($age)
    {       
        try {
            $this->age=$age;
        }
        catch(Exception $e)
        {
            if ($age > 20)
            {
                throw new Exeption("age exceeds",$age, $e->getMessage());
            }
        }               
    }
    function group($group)
    {
        return $this->group=$group;
    }
}

$doerte=new behaviour();  
$doerte ->people(array('male','female'));
$doerte ->animals(array('fish','whale'));
$doerte ->bird(array('parrot','crow'));
$doerte->non_human('alien');
$doerte->read_age('23');
$doerte->group('living_things');
//$doerte->__autoload();
print_r($doerte);
?>

【问题讨论】:

  • 如果你拼写正确Exception 会有帮助.... 它不是Exeption.... 但是你为什么要把它扔到一个catch块里呢?你的 try/catch 应该在主代码中,而不是在 read_age 方法中,它应该只是抛出异常

标签: php oop exception-handling


【解决方案1】:

您检查过您的 PHP 错误日志吗?可能是拼写错误的“Exeption”,应该是“Exception”=> new \Exception(...

@Debflav:抱歉发了双重帖子。你的答案当时没有出现在我的屏幕上

【讨论】:

  • 嗨,实际上我犯了这个错误,但在编码中我正确地写为“exeption”:)
【解决方案2】:

是否要在年龄超过 20 岁时创建异常?这里可能是一个解决方案(部分代码):

/* ... */
function read_age($age)
    {       
        if ($age > 20) {
            throw new Exception("age exceeds, shoulw be less than 20");
        } else {
            $this->age=$age;
        }
    }
/* ... */

try {
    $doerte=new behaviour();  
    $doerte ->people(array('male','female'));
    $doerte ->animals(array('fish','whale'));
    $doerte ->bird(array('parrot','crow'));
    $doerte->non_human('alien');
    $doerte->read_age('23');
    $doerte->group('living_things');
    echo "It's all right";
    print_r($doerte);
} catch (Exception $e) {
    echo "Something went wrong: ".$e->getMessage();
}

【讨论】:

  • 你的代码可以正常工作,但我想它不正确只是获取 try 块中的所有对象?
  • 理想情况下,您应该只将有风险的操作放入 try{} 块。并尝试将尽可能低的代码放在那里。我刚刚复制了您的代码,假设在您的情况下创建对象是有风险的操作。你可以保留 $doerte->read_age('23');在 try{} 中,它也可以工作。
  • 是的,但正如您的例外消息仅针对年龄对象建议的那样,因此如果发生任何错误,它将生成相同的例外,对吗?
  • 您可以将抛出异常添加到任何 setter。例如添加一些验证并将新的异常('Wrong group')抛出到行为::组(),然后你会在catch块中看到相应的消息。
【解决方案3】:

你在错误的地方抛出异常。

如果有些东西是无效的,你需要扔掉。那属于尝试部分。 catch 部分用于处理异常。

这样想:如果您的程序必须大声寻求帮助,您就会抛出异常,因为它不知道如何处理数据(尝试)。应该使用异常块来处理求救声

function read_age($age)
{       
    try {
        if($age > 20) {
            throw new Exception('Too old!');
        }
        $this->age=$age;
    }
    catch(Exception $e)
    {
        echo 'There has been an error: '.$e->getMessage();
    }               
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2019-05-07
    • 2019-05-20
    • 2011-03-03
    相关资源
    最近更新 更多