php – 为什么不能将SplFileInfo转换为布尔值?
发布时间:2020-12-13 18:18:57 所属栏目:PHP教程 来源:网络整理
导读:PHP的一个限制是对象总是评估为true.但是,SplFileinfo(以及Symfony的UploadedFile等子类)的行为有所不同: $a = new ArrayIterator(); // or any other class$b = new SplFileInfo(__FILE__); // file used is not importantif ($a) echo 'true'; // 'true'i
PHP的一个限制是对象总是评估为true.但是,SplFileinfo(以及Symfony的UploadedFile等子类)的行为有所不同:
$a = new ArrayIterator(); // or any other class $b = new SplFileInfo(__FILE__); // file used is not important if ($a) echo 'true'; // 'true' if (!$a) echo 'false'; // nothing because $a is true if ($b) echo 'true'; // 'true' if (!$b) echo 'false'; // Catchable fatal error: Object of class // SplFileInfo could not be converted to boolean 这是一个错误吗?测试在5.3和5.4. SplFileObject也会发生这种情况.可能related question.和2011年的a Symfony issue.
我觉得这是一个错误所以我提交了一份错误报告.
https://bugs.php.net/bug.php?id=65213 – 编辑,大约在PHP 5.6.17附近,这个错误似乎已得到修复. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |