Req #64782 [Opn->Csd]: SplFileObject constructor make $context optional / give it a default value
| From: | nikic@php.net | Date: | Thu, 12 Sep 2013 15:47:59 +0000 |
| Subject: | Req #64782 [Opn->Csd]: SplFileObject constructor make $context optional / give it a default value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-181551@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64782&edit=1
ID: 64782
Updated by: nikic@php.net
Reported by: hanskrentel at yahoo dot de
Summary: SplFileObject constructor make $context optional /
give it a default value
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: SPL related
PHP Version: 5.4.14
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ad976d82405ad25fb424328c5b9bdca7b734cca4
Log: Fix bug #64782: SplFileObject constructor make $context optional
Previous Comments:
------------------------------------------------------------------------
[2013-05-09 03:11:43] laruence@php.net
The following patch has been added/updated:
Patch Name: accept_null_for_context.patch
Revision: 1368069103
URL: https://bugs.php.net/patch-display.php?bug=64782&patch=accept_null_for_context.patch&revision=1368069103
------------------------------------------------------------------------
[2013-05-09 03:10:59] laruence@php.net
all I got is:
PHP Fatal error: Uncaught exception 'RuntimeException' with message
'SplFileObject::__construct() expects parameter 4 to be resource, null given' in
/tmp/1.php:6
Stack trace:
#0 /tmp/1.php(6): SplFileObject->__construct('/tmp/1.php', 'r', false, NULL)
#1 /tmp/1.php(10): Myfile->__construct('/tmp/1.php')
#2 {main}
thrown in /tmp/1.php on line 6
it you meant this err, yes, this could be improved. I will attach a patch
------------------------------------------------------------------------
[2013-05-07 10:02:57] hanskrentel at yahoo dot de
Correction: The line "$this->levels = new Levels();" in the test-script above
needs to be removed.
Addendum: The following variant shows the boilerplate code this needs to get
away with the error:
<?php
class Myfile extends SplFileObject
{
public function __construct($file_name, $open_mode = "r", $use_include_path
= FALSE, $context = NULL) {
if ($context === NULL) {
parent::__construct($file_name, $open_mode, $use_include_path);
} else {
parent::__construct($file_name, $open_mode, $use_include_path,
$context);
}
}
}
------------------------------------------------------------------------
[2013-05-07 09:57:03] hanskrentel at yahoo dot de
Description:
------------
When extending from SplFileObject and overwriting the constructor, it is not
easily possible to override the parent one because for the last parameter $context
one can not provide an optional default.
Therefore it requires (somewhat needles) if-branched code only to deal with the
$context not passed case when calling the parents constructor.
It would be nice if $context does accept NULL then if I do not want to use any
context but need to specify the parameter.
Test script:
---------------
<?php
class Myfile extends SplFileObject
{
public function __construct($file_name, $open_mode = "r", $use_include_path = FALSE,
$context = NULL) {
$this->levels = new Levels();
parent::__construct($file_name, $open_mode, $use_include_path, $context);
}
}
$file = new MyFile(__FILE__);
Expected result:
----------------
It should not give any warning or error.
Actual result:
--------------
Warning: Missing argument 4 for Myfile::__construct(), called in [pointing to the
line "$file = new MyFile(__FILE__);"] and defined in [pointing to the line
"public function __construct($file_name, $open_mode = "r", $use_include_path =
FALSE, $context = NULL) {"]
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64782&edit=1