Edit report at https://bugs.php.net/bug.php?id=64782&edit=1
ID: 64782 Updated by: larue...@php.net 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: 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 Previous Comments: ------------------------------------------------------------------------ [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