One of the limitations of PHP is that objects always evaluate to true
. However SplFileinfo
(and subclasses such as Symfony's UploadedFile
) behave differently:
$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
Is this a bug? Tested in 5.3 and 5.4. Also happens with SplFileObject
. Possible related question. And a Symfony issue from 2011.
I feel it's a bug so I filed a bug report.
https://bugs.php.net/bug.php?id=65213
-- Edit, somewhere roughly around php 5.6.17 this bug seems to have been fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With