Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't SplFileInfo be converted to boolean?

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.

like image 217
Tamlyn Avatar asked Jul 05 '13 12:07

Tamlyn


1 Answers

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.

like image 137
goat Avatar answered Sep 29 '22 03:09

goat