appu
appu

Reputation: 97

PHP Exception Handler Not producing any error

Hy,

The code below give does not throw an exception as expected instead

   <?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";
       }

     }

     ?>

The above is saved as class.propertyObject.php and then called from another file test.php. The code for test.php is as follows:

 <?php

   include('class.propertyObject.php');

   $newObj = new propertyObject();

   $newObj->name='Ryann';
   $newObj->dateofbirth='08/01/2009';

   $newObj->sayHello();
   $newObj->dateofBirth='hello';

?>

Output is: Hello! My name is Ryann and my D.O.B is 08/01/2009.

In my opinion the last statement $newObj->dateofBirth='hello'; should throw an exception and accordingly error message should be displayed however it does not give out any error. Moreover, I changed the value in the following $newObj->dateofbirth='08/01/2009'; to a string name such as john it outputs: Hello! My name is Ryann and my D.O.B is john. Why there is no exception message displayed for the last statement or why the function setdateofBirth($dob) does not throw any exception when non date string value is set as $dob.

Upvotes: 0

Views: 149

Answers (2)

roman
roman

Reputation: 11278

well dont know if it is the formatting, but you are using

_set() and _get()

instead of

__set() and __get()

so php just creates a new property dateOfBirth for your object and never even touches your $_properties array ...

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 301135

The documentation for strtotime says this

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

So I would guess you are using a PHP version greater than 5.1. Simply check the return value of strtotime for false, rather than -1.

Upvotes: 0

Related Questions