Bug #69275 [Opn->Asn]: SplFileObject constructor cannot be overridden

From: Date: Sun, 22 Mar 2015 19:23:01 +0000
Subject: Bug #69275 [Opn->Asn]: SplFileObject constructor cannot be overridden
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191529@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69275&edit=1

 ID:                 69275
 Updated by:         danack@php.net
 Reported by:        stephen dot reay at me dot com
 Summary:            SplFileObject constructor cannot be overridden
-Status:             Open
+Status:             Assigned
 Type:               Bug
 Package:            *General Issues
 Operating System:   OS X, Linux
 PHP Version:        5.6.7
-Assigned To:        
+Assigned To:        danack
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2015-03-22 07:41:53] 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



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69275&edit=1


Thread (7 messages)

« previous php.bugs (#191529) next »