tags:

views:

100

answers:

1

I'm using PHP's new(ish) Date classes for a calendar/scheduler I'm developing. I am trying to extend DateInterval so that I limit the interval to particular sizes like 1 year, 1 month, 1 week or 1 day. The extended class would be used to help generate the calendar view.

I'm getting the PHP error: Unknown property (days) when I run the following method.

private function adjust(){

     //to zero all the DateInterval properties except the one I want
    $adjustments = array ('y'=>0, 'm'=>0, 'd'=>0, 
                           'h'=>0, 'i'=>0, 's'=>0, 
                           'invert'=>0, 'days'=>0); 


    if($this->y>=1 || $this->days>180){
     $adjustments['y']=1; //1 year
    } else if ($this->m>6){
     $adjustments['y']=1; //1 year
    } else if ($this->m>=1){
     $adjustments['m']=$this->m; //multiple months less than 6
    } else if ($this->d>7){
     $adjustments['m']=1; //1 month
    } else if ($this->d>1){
     $adjustments['d']=7; //1 week
    } else {
     $adjustments['d']=1; //1 day
    }

    foreach($adjustments as $k=>$v){
     $this->$k=$v; //reset all the class properties
    }  
}

Any idea why? As far as the documentation seems to suggest, 'days' should be valid.

+1  A: 

Actually, if you look more closely, DateInterval really DOESN'T have a class property named 'days'. These are "pseudo-properties", like the attributes on a SimpleXML object, and trying to extend these built-in classes from userland code isn't a straight-forward as you'd like it to be. Take a look at the Reflection class and its friends for help.

TML
Thanks for the pointer. I went and looked around but there isn't a lot beyond the method signatures. Do you know of a good reference for concrete (as opposed to test) examples of using Reflection (doesn't have to be PHP)? My grasp of the whys and wherefores of Reflection is weak at best.
dnagirl