|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-05-07 09:57 UTC] 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) {"]
Patchesaccept_null_for_context.patch (last revision 2013-05-09 03:11 UTC by laruence@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 03:00:02 2025 UTC |
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); } } }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