|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69275 SplFileObject constructor cannot be overridden
Submitted: 2015-03-22 07:41 UTC Modified: 2020-11-07 13:24 UTC
Avg. Score:4.5 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:3 (100.0%)
From: stephen dot reay at me dot com Assigned: danack (profile)
Status: Assigned Package: *General Issues
PHP Version: 5.6.7 OS: OS X, Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-03-22 07:41 UTC] stephen dot reay at me dot com
A class that extends SplFileObject,  specifies its own constructor and is registered with SplFileInfo::setFileClass() will only be instantiated with the first argument passed to SplFileInfo::openFile().

Test script:

class FileObject extends \SplFileObject {

	public function __construct($file_name, $open_mode = 'r', $use_include_path = false, $context = null) {
		if (is_resource($context)) {
			parent::__construct($file_name, $open_mode, $use_include_path, $context);
		else {
			parent::__construct($file_name, $open_mode, $use_include_path);

$file = new \SplFileInfo(__DIR__ . DIRECTORY_SEPARATOR . 'foo');



Expected result:
File is created

Actual result:
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(/Users/stephen/Projects/Koalephant/Bamboo/tests/foo): failed to open stream: No such file or directory' in /Users/stephen/Projects/Koalephant/Bamboo/tests/SplFileInfoTest.php:10
Stack trace:
#0 /Users/stephen/Projects/Koalephant/Bamboo/tests/SplFileInfoTest.php(10): SplFileObject->__construct('/Users/stephen/...', 'r', false)
#1 [internal function]: FileObject->__construct('/Users/stephen/...', 'r')
#2 /Users/stephen/Projects/Koalephant/Bamboo/tests/SplFileInfoTest.php(19): SplFileInfo->openFile('w+')
#3 {main}
  thrown in /Users/stephen/Projects/Koalephant/Bamboo/tests/SplFileInfoTest.php on line 10


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-22 19:23 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: danack
 [2015-03-22 19:23 UTC]
The cause is pretty clear. When the class is the standard SplFileObject the parameters passed to openFile are read and passed to the file open call.

When the class isn't that class, the params aren't parsed and instead the mode is set to 'r' only:

Which obviously causes the file opening to fail, as the file doesn't exist.
 [2015-03-24 05:44 UTC]
@danack are you able to make a patch? thanks
 [2015-03-24 13:15 UTC]
I'm looking at it yes.

Whether I'm able to fix it, is a question yet to be determined.
 [2015-03-24 19:51 UTC]
PR is attached - as it changes the Zend engine to allow functions that contain more than 2 parameters to be easily callable, it needs some more eyes on it.
 [2018-08-19 09:30 UTC] stephen dot reay at me dot com
It's been over three years since the patch was attached, and despite the GH PR being closed as "targets a security fix only branch", this is still present in 7.3.

Is there any chance of getting more eyeballs on this before the end of the decade?
 [2020-11-07 13:24 UTC]
This issue has been fixed with commit fe88d23914[1], available as
of PHP 8.0.0.

[1] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Oct 22 23:03:34 2021 UTC