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.