php.net |  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: 2021-12-02 17:09 UTC
Votes:4
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: Closed Package: SPL related
PHP Version: 5.6.7 OS: OS X, Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: stephen dot reay at me dot com
New email:
PHP Version: OS:

 

 [2015-03-22 07:41 UTC] stephen dot reay at me dot com
Description:
------------
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:
---------------
<?php

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');

$file->setFileClass('FileObject');

$file->openFile('w+');

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


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-22 19:23 UTC] danack@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: danack
 [2015-03-22 19:23 UTC] danack@php.net
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.
http://lxr.php.net/xref/PHP_5_6/ext/spl/spl_directory.c#537

When the class isn't that class, the params aren't parsed and instead the mode is set to 'r' only:
http://lxr.php.net/xref/PHP_5_6/ext/spl/spl_directory.c#524

Which obviously causes the file opening to fail, as the file doesn't exist.
 [2015-03-24 05:44 UTC] laruence@php.net
@danack are you able to make a patch? thanks
 [2015-03-24 13:15 UTC] danack@php.net
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] danack@php.net
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] cmb@php.net
This issue has been fixed with commit fe88d23914[1], available as
of PHP 8.0.0.

[1] <https://github.com/php/php-src/commit/fe88d23914e4185daffe858d01067beada34cdca>
 [2021-12-02 17:09 UTC] cmb@php.net
-Status: Assigned +Status: Closed -Package: *General Issues +Package: SPL related
 [2021-12-02 17:09 UTC] cmb@php.net
Since it's too late to backport to PHP-7.4 anyway, I'm closing
this ticket.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 14:01:37 2025 UTC