El controlador de excepciones de PHP no produce ningún 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.

preguntado el 09 de enero de 11 a las 10:01

2 Respuestas

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

_set() and _get()

en lugar de

__set() and __get()

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

Respondido el 09 de enero de 11 a las 14:01

La documentación para strtotime dice esto

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.

Respondido el 09 de enero de 11 a las 13:01

Ah No does not work. I tried the following: (strtotime($dob) == false) , (strtotime($dob) == 'false'), (!strtotime($dob)) but none of them work. the outputs is still: Hello! My name is Ryann and my D.O.B is john. - appu

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.