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);
?>
Do you want to create an exception when age exceeds 20? Here could be a solution (partial code):
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
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