Req #64782 [Com]: SplFileObject constructor make $context optional / give it a default value
| From: | hanskrentel at yahoo dot de | Date: | Tue, 07 May 2013 10:02:57 +0000 |
| Subject: | Req #64782 [Com]: SplFileObject constructor make $context optional / give it a default value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-178827@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
Comment by: hanskrentel at yahoo dot de
Reported by: hanskrentel at yahoo dot de
Summary: SplFileObject constructor make $context optional /
give it a default value
Status: Open
Type: Feature/Change Request
Package: SPL related
PHP Version: 5.4.14
Block user comment: N
Private report: N
New Comment:
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);
}
}
}
Previous Comments:
------------------------------------------------------------------------
[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