のHyを、生成していないPHPの例外ハンドラは、エラー
上記他からclass.propertyObject.phpとして保存し、その後に呼び出され<?php
class propertyObject {
private $_properties = array('name' => null , 'dateofBirth' => null);
function _get($propertyName)
{
if(!array_key_exists($propertyName, $this->_properties))
{
throw new Exception("Invalid Property Value");
}
if(method_exists($this,'get'.$propertyName))
{
return call_user_func(array($this, 'get'.$propertyName));
}
else
{
return $this->_properties[$propertyName];
}
}
function _set($propertyName, $value)
{
if(!array_key_exists($propertyName, $this->_properties))
{
throw new Exception("The property value you are trying to set is not valid");
}
if(method_exists($this, 'set'.$propertyName))
{
return call_user_func(array($this,'set'.$propertyName));
}
else
{
return $this->_properties[$propertyName]=$value;
}
}
function setdateofBirth($dob)
{
if(strtotime($dob) == -1)
{
throw new Exception ("Invalid Date of Birth. Please enter a value date");
}
$this->_properties['dateofBirth']=$dob;
}
function sayHello()
{
echo "Hello! My name is $this->name and my D.O.B is $this->dateofBirth";
}
}
?>
代わりに、期待通りに以下のコードが例外をスローしません与える
ファイルtest.php。 test.phpのコードは次のとおりです。
<?php
include('class.propertyObject.php');
$newObj = new propertyObject();
$newObj->name='Ryann';
$newObj->dateofbirth='08/01/2009';
$newObj->sayHello();
$newObj->dateofBirth='hello';
?>
出力はHello!私の名前はRyannで、私のD.O.Bは08/01/2009です。
私の意見では、$ newObj-> dateofBirth = 'hello'という最後のステートメントです。例外をスローする必要があります。したがって、エラーメッセージは表示されるはずですが、エラーは発生しません。さらに、次の$ newObj-> dateofbirth = '08/01/2009 'の値を変更しました。ジョンなどの文字列名に出力します。こんにちは!私の名前はライアン、私のD.O.Bはジョンです。最後のステートメントの例外メッセージが表示されない理由、または日付以外の文字列の値が$ dobに設定されているとsetdateofBirth($ dob)関数が例外をスローしないのはなぜですか?
は
ああ優秀。 10/10。イーグルの目は...ありがとう。 – appu
優れています。 10/10。イーグルの目は...ありがとう。 – appu