Exception handling in oop PHP not working

老子叫甜甜 提交于 2020-01-05 04:36:10

问题


I am very new to object oriented PHP and trying some basic examples to get good hand on oop php. I have simple exmaple above in which i am trying to learn exception handling and generate an exception error message when age is greater than 20 but not working.

<?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);
?>

回答1:


you throw the exception at the wrong place.

If something is invalid you need to throw. That belongs to the try part. The catch part is used to handle the exception.

Think of it that way: you trow an exception if you programm has to yell out for help since it doesnt know what to do with data (try). The exception block should be used to take care of that cry for help

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();
    }               
}



回答2:


Do you want to create an exception when age exceeds 20? Here could be a solution (partial code):

/* ... */
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();
}



回答3:


Have you checked your PHP error logs? Might be the misspelled "Exeption" which should be "Exception" => new \Exception(...

@Debflav: sorry for the double post. your answere hasn't shown up on my screen at the time



来源:https://stackoverflow.com/questions/25504877/exception-handling-in-oop-php-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!