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: 2015-03-24 05:44 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

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.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC