PHP DateTime DateInterval isset changes after var_

2019-05-09 22:08发布

问题:

Any variable in this object is !isset() but if I either var_dump($interval) or print_r($interval), these variables becomes isset(). This also applies to empty()/!empty().

So in the code below $interval->i is initially !isset() but isset() after I var_dump($interval).

$future = new DateTime("2018-08-24");
$now = new DateTime();

$interval = $future->diff($now);

if (isset($interval->i)) {
    echo 'isset' . $interval->i;
} else {
    echo 'not isset' . $interval->i;
}

var_dump($interval);

if (isset($interval->i)) {
    echo 'isset' . $interval->i;
} else {
    echo 'not isset' . $interval->i;
}

What could possibly be causing these to be !isset and empty initially, but isset and !empty afterwards?

回答1:

I was able to reproduce the same error. You can even swap out isset() with property_exists() and get the same strange behavior.

Did a little searching in php's bug database and it looks like they fixed it in version 7.0.5:

https://bugs.php.net/bug.php?id=69587



回答2:

Am not sure which version of PHP you are using but Use PHP version 7.0.5. Its a bug.

This should be useful: https://bugs.php.net/bug.php?id=69587