PHP DateTime DateInterval isset changes after var_dump

会有一股神秘感。 提交于 2020-01-02 05:34:10

问题


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



来源:https://stackoverflow.com/questions/45871847/php-datetime-dateinterval-isset-changes-after-var-dump

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!