|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55300 \DirectoryIterator, parent::__construct() and \LogicException
Submitted: 2011-07-27 15:36 UTC Modified: 2011-10-29 23:18 UTC
Avg. Score:4.5 ± 0.8
Reproduced:10 of 10 (100.0%)
Same Version:10 (100.0%)
Same OS:8 (80.0%)
From: frederic dot hardy at mageekbox dot net Assigned: cataphract (profile)
Status: Closed Package: SPL related
PHP Version: 5.4.0alpha2 OS: Linux
Private report: No CVE-ID: None
 [2011-07-27 15:36 UTC] frederic dot hardy at mageekbox dot net
In PHP 5.4, an exception was throwed if a call to parent::__construct() is not done in the constructor of a class which extends \DirectoryIterator.
Exception was not throwed in previous version of PHP, so it's a BC break.
And this BC break must be annoying in unit test, because it's impossible to mock the \DirectoryIterator class.

Test script:

class myDirectoryIterator extends \DirectoryIterator
	public function __construct()

$directoryIterator = new myDirectoryIterator();


Expected result:

Actual result:
Fatal error: Uncaught exception 'LogicException' with message 'In the constructor of myDirectoryIterator, parent::__construct() must be called and its exceptions cannot be cleared' in /path/to/directoryIterator.php:10
Stack trace:
#0 /path/to/directoryIterator.php(10): myDirectoryIterator->internal_construction_wrapper()
#1 {main}
  thrown in /path/to/directoryIterator.php on line 10


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-27 23:30 UTC]
If you don't call the parent constructor, the class has no functionality. Why would you want to do this?
 [2011-07-28 07:27 UTC] frederic dot hardy at mageekbox dot net
This bug seems to be related to #54384.
And i want to do this to mock a \DirectoryIterator instance, to "simulate" a directory on the file system.
 [2011-07-28 08:29 UTC]
Why can't you use a user stream wrapper then? Maybe DirectoryIterator should be an interface, but it isn't.
 [2011-07-28 08:44 UTC]
Thing is that it is a bc break, whether it was well designed in the 1st place or 
not is sadly not relevant here.

Is there any way to restore this behavior without bc breaks?
 [2011-08-04 06:56 UTC] frederic dot hardy at mageekbox dot net
This BC break is a problem with the symfony 2 framework.
All command throw this exception :

  In the constructor of Symfony\Component\Finder\SplFileInfo, parent::__construct() must be called and its exceptions cannot be cleared
 [2011-08-06 01:09 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: cataphract
 [2011-08-09 08:44 UTC]
The main issue is not that it's a BC break I'd say, it's that apparently adding the call to parent::__construct does not fix the issue, the exception is always thrown if you try to have your own constructor in the extending class.

BTW: This is a duplicate of IMO.
 [2011-08-09 08:46 UTC]
Actually according to my test case in - it's even happening if you don't redefine the constructor at all. It prevents extending the class altogether, which makes no sense to me.
 [2011-08-09 08:52 UTC]
I wouldn't consider these duplicates. #55175 is an actual problem, this is simply an intended BC break that turned out to have a larger impact than I expected. I'll take care of both not after the beginning of next week.
 [2011-09-02 20:03 UTC] krossekrabbe at gmail dot com
No, it's not only a BC break. It's exactly the same, it is not possible to extend 
the class. Not when calling parent::__construct(), nor when not overriding 
__construct() at all.
 [2011-09-14 15:38 UTC] frederic dot hardy at mageekbox dot net
\Phar has the same problem, because this class extends \recursiveDirectoryIterator.
And stream wrapper can not be used with it because "Phar objects can only be created from local files".
 [2011-10-29 23:17 UTC]
Automatic comment from SVN on behalf of cataphract
Log: - Reverted changes that required constructor overrides to invoke the parent
  constructor in several SPL classes and applied 5.3 fixes instead.
  Related bugs: #54384, #55175 and #55300
 [2011-10-29 23:18 UTC]
Replaced with 5.3 code.
 [2011-10-29 23:18 UTC]
-Status: Assigned +Status: Closed
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC