php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64782 SplFileObject constructor make $context optional / give it a default value
Submitted: 2013-05-07 09:57 UTC Modified: 2013-05-09 03:10 UTC
From: hanskrentel at yahoo dot de Assigned:
Status: Closed Package: SPL related
PHP Version: 5.4.14 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hanskrentel at yahoo dot de
New email:
PHP Version: OS:

 

 [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) {"]

Patches

accept_null_for_context.patch (last revision 2013-05-09 03:11 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-07 10:02 UTC] 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-09 03:10 UTC] 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-09 03:11 UTC] 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-09-12 15:47 UTC] nikic@php.net
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
 [2013-09-12 15:47 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2013-09-12 15:49 UTC] nikic@php.net
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
 [2013-11-17 09:30 UTC] laruence@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC