Edit report at https://bugs.php.net/bug.php?id=65654&edit=1
ID: 65654
Comment by: steven at gowebprint dot com
Reported by: steven at gowebprint dot com
Summary: Extending DateInterval and unable to use $this->days
Status: Open
Type: Bug
Package: Date/time related
Operating System: Cent OS 6.4
PHP Version: 5.4.19
Block user comment: N
Private report: N
New Comment:
You are right.
I have extended DateTime object and overrides the diff method with
public function diff ($datetime2 , $absolute = false){
return new DateInterval(parent::diff($datetime2,$absolute));
}
If you look at the sample code that would be return the DateInterval in the
test namespace.
Since I am overriding the diff method I want to return a DateInterval object
that behaves like it would from \DateTime which includes a populated days
property , but I have no way of setting the days property or telling \DateTime
to return a different DateInterval object, so I am unable to return a
DateInteral object with a days property filled and returned from the diff
method as expected.
The manual says:
If the DateInterval object was created by DateTime::diff(), then this is the
total number of days between the start and end dates. Otherwise, days will be
FALSE.
This is the method I have overridden and I am trying to return a extended
DateInterval object, whilst keeping the all of the DateInterval property's
intact including days.
Previous Comments:
------------------------------------------------------------------------
[2013-09-11 13:36:48] [email protected]
"I am trying to extend DateTime and DaveInterval to add extra functionality to
the core classes, but I have been unable to write to $this->days internally to
the object."
That's not really a bug. "days" is a calculated version that only make sense in
the context of having compared two other DateTime objects. Without those other
two objects presents, its value makes no sense.
------------------------------------------------------------------------
[2013-09-11 13:26:49] steven at gowebprint dot com
Description:
------------
I am trying to extend DateTime and DaveInterval to add extra functionality to
the core classes, but I have been unable to write to $this->days internally to
the object.
As a side note, I have been able to cause a seg fault within our application
when writing to $interval->days = 100 externally, but unable to reproduce on a
small script.
Test script:
---------------
http://pastebin.com/tBay704K
Expected result:
----------------
object(DateInterval)[3]
public 'y' => int 1
public 'm' => int 0
public 'd' => int 0
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 1
public 'days' => int 365
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0
object(test\DateInterval)[4]
public 'days' => int 365
public 'y' => int 1
public 'm' => int 0
public 'd' => int 0
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 1
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0
Actual result:
--------------
object(DateInterval)[3]
public 'y' => int 1
public 'm' => int 0
public 'd' => int 0
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 1
public 'days' => int 365
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0
object(test\DateInterval)[4]
public 'days' => boolean false
public 'y' => int 1
public 'm' => int 0
public 'd' => int 0
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 1
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65654&edit=1