【发布时间】: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