Bug #71053 [Opn->Csd]: Type Hint about DateTime::diff does not match with informed

From: Date: Wed, 09 Dec 2015 03:41:26 +0000
Subject: Bug #71053 [Opn->Csd]: Type Hint about DateTime::diff does not match with informed
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197717@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71053&edit=1 ID: 71053 Updated by: reeze@php.net Reported by: gabriel dot heming at hotmail dot com Summary: Type Hint about DateTime::diff does not match with informed -Status: Open +Status: Closed Type: Bug Package: Date/time related Operating System: Debian 8.2 PHP Version: 7.0.0 Block user comment: N Private report: N New Comment: Automatic comment on behalf of reeze Revision: http://git.php.net/?p=php-src.git;a=commit;h=943b23dcf6666a708c2ad4470cad5db36d47683a Log: Fixed bug #71053 (Type Hint about DateTime::diff does not match with informed) Previous Comments: ------------------------------------------------------------------------ [2015-12-08 11:06:31] gabriel dot heming at hotmail dot com Thanks about the reply. I'll keep an eye in boths reports. I don't know if it matters, but the compatibility error occurs also for the type hint \DateTimeInterface $object. ------------------------------------------------------------------------ [2015-12-07 21:12:17] requinix@php.net Looking at the code, there's no reason why $absolute should be anything but a boolean - all it does when enabled is turn off the invert flag on the interval object. So I think this should be addressed in the code, not the documentation. As for the compatibility error, the bool in "bool $absolute" is not actually part of the method signature. The documentation is telling you that the parameter must be (strict) or will be coerced to (loose) a boolean, but the parameter itself is only defined as "$absolute". Keep an eye on bug #71051. ------------------------------------------------------------------------ [2015-12-07 20:19:07] gabriel dot heming at hotmail dot com The same strange behavior occurs when try to inherits DateTime: Declaration of MyDateTime::diff(DateTimeInterface $object, bool $absolute = false) must be compatible with DateTimeInterface::diff($object, $absolute = NULL) ------------------------------------------------------------------------ [2015-12-07 20:08:15] gabriel dot heming at hotmail dot com Description: ------------ The second parameter (or third one) of method DateTime::diff must be a bool (as described in php documentation). AS can see below http://php.net/manual/en/datetime.diff.php Object oriented style: public DateInterval DateTime::diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] ); public DateInterval DateTimeImmutable::diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] ); public DateInterval DateTimeInterface::diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] ); Test script: --------------- <?php declare(strict_types=1); $date1 = clone $date2 = new \DateTime(); var_dump($date1->diff($date2 , true)); Expected result: ---------------- object(DateInterval)#3 (15) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> int(0) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } Actual result: -------------- Fatal error: Uncaught TypeError: DateTime::diff() expects parameter 2 to be integer, boolean given in /code/3uk3Qo:6 Stack trace: #0 /code/3uk3Qo(6): DateTime->diff(Object(DateTime), true) #1 {main} thrown in /code/3uk3Qo on line 6 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71053&edit=1

« previous php.bugs (#197717) next »