php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66079 Exception constructor might ignore default arguments when overriden
Submitted: 2013-11-11 21:55 UTC Modified: 2016-09-29 10:38 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: cmbecker69 at gmx dot de Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
 [2013-11-11 21:55 UTC] cmbecker69 at gmx dot de
Description:
------------
When overriden, calling the constructor of class Exception 
ignores the default arguments, if the properties $code and 
$message are already set.

From looking at the implementation of the constructor[1], 
the behavior is to be expected, because when the default 
values are passed for $code and $message, zend_update_property_*()
is not called.

I am fully aware that the given test script shows a bad 
programming practise, but it would work for user defined 
classes, and the unusual behavior of the Exception constructor 
doesn't seem to be documented.  At least this should be done, IMHO.

[1] <http://lxr.php.net/xref/PHP_5_5/Zend/zend_exceptions.c#223>

Test script:
---------------
// cf. <http://3v4l.org/3Ylvi#v430>

class MyException extends Exception
{
    function __construct()
    {
        $this->code = 42;
        $this->message = "foo";
        parent::__construct();
    }
}

try {
    throw new MyException();
} catch (MyException $e) {
    var_dump($e->getCode());
    var_dump($e->getMessage());
}


Expected result:
----------------
int(0)
string(0) ""

Actual result:
--------------
int(42)
string(3) "foo"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-10 02:12 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2016-09-29 10:34 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2016-09-29 10:34 UTC] cmb@php.net
We can't easily change the current behavior for BC reasons, so I'm
switching to doc bug.
 [2016-09-29 10:38 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=340256
Log: Fix #66079: Exception constructor might ignore default arguments when overriden
 [2016-09-29 10:38 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-02-07 06:06 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=98a2748836e80225a5c07ac4617950da529456d1
Log: Fix #66079: Exception constructor might ignore default arguments when overriden
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 23 05:01:31 2024 UTC