Hy,
Yerine beklendiği gibi al gülüm ver gülüm Aşağıdaki kod, bir istisna değil
<?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";
}
}
?>
Yukarıdaki class.propertyObject.php olarak kaydedilir ve daha sonra başka bir dosya test.php denir. Aşağıdaki gibi test.php için kodu:
<?php
include('class.propertyObject.php');
$newObj = new propertyObject();
$newObj->name='Ryann';
$newObj->dateofbirth='08/01/2009';
$newObj->sayHello();
$newObj->dateofBirth='hello';
?>
Output: Hello! Benim adım Ryann ve benim D.O.B 08/01/2009 olduğunu.
Bence son deyimi $ newObj-> DateOfBirth = 'merhaba'; bir istisna olmalı ve buna göre bir hata iletisi görüntülenir gerektiğini ancak herhangi bir hata dışarı vermez. Ayrıca, ben şu $ newObj-> DateOfBirth = '08 / 01/2009 'de değerini değişti; Böyle john gibi bir dize adına bu çıkışları: Hello! Benim adım Ryann ve benim D.O.B john. Son açıklamada da neden olmayan tarih dize değeri $ dob olarak ayarlandığında fonksiyon setdateofBirth ($ DOB) herhangi bir istisna değil görüntülenir istisna mesajı var neden.