php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65213 cannot cast SplFileInfo to boolean
Submitted: 2013-07-06 14:23 UTC Modified: 2013-07-07 15:56 UTC
Votes:13
Avg. Score:4.5 ± 0.7
Reproduced:12 of 12 (100.0%)
Same Version:4 (33.3%)
Same OS:3 (25.0%)
From: fake at example dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 5.5.0 OS:
Private report: No CVE-ID: None
 [2013-07-06 14:23 UTC] fake at example dot com
Description:
------------
Casting an instance of SplFileInfo to boolean throws an exception. It should behave like other objects, where the cast always results in boolean true without throwing. Otherwise, it breaks common code like

if (!$myObj) {...}

Test script:
---------------
<?php
$o = new SplFileInfo('.');
var_dump((bool) $o);

Expected result:
----------------
I expect (bool) true as output.

Actual result:
--------------
Catchable fatal error: Object of class SplFileInfo could not be converted to boolean in ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-07 15:56 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2013-07-07 15:56 UTC] laruence@php.net
change to FR
 [2014-02-27 03:49 UTC] levim@php.net
This seems to have worked in PHP 5.1 when SplFileInfo was created. It has been broken since PHP 5.2
 [2014-05-11 00:21 UTC] wmpalmer at gmail dot com
I note that this has been listed as "Type: Feature/Change Request", can you explain the rationale behind this? This clearly goes against the documented behaviour of objects in a boolean context, so some clarification of *why* this was changed to FR could be useful.
 [2014-08-20 13:45 UTC] bpolaszek at gmail dot com
Hello,

I've just been confronted to the same problem and I also don't understand why this has been changed to a feature request.

This issue goes against the documentation about booleans - http://php.net/manual/en/language.types.boolean.php - and is indeed a bug, or at least an unexpected behavior with a critical matter (it just throws... a fatal error).

Thanks for reconsidering the issue.
Beno!t POLASZEK
 [2014-12-03 23:27 UTC] datibbaw@php.net
Automatic comment on behalf of datibbaw
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2bcf8a6cd921486f0ee4835166984ecf4f0b2021
Log: Fixed #65213 - cannot cast SplFileInfo to boolean
 [2014-12-03 23:27 UTC] datibbaw@php.net
-Status: Open +Status: Closed
 [2014-12-05 07:00 UTC] ab@php.net
Automatic comment on behalf of datibbaw
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2bcf8a6cd921486f0ee4835166984ecf4f0b2021
Log: Fixed #65213 - cannot cast SplFileInfo to boolean
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC