0

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.

2 Answers 2

1

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 ...

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.