php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51406 Exception constructor calling method statically populates $this
Submitted: 2010-03-26 21:16 UTC Modified: 2010-04-02 12:51 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: sommertm at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.2 OS: Windows Server 2008
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sommertm at gmail dot com
New email:
PHP Version: OS:

 

 [2010-03-26 21:16 UTC] sommertm at gmail dot com
Description:
------------
You are able to call a class method statically even if it is not defined as static. If you do this from the constructor of an Exception, $this references the Exception object. This is tested on my dev machine running Windows 7, IIS7.5 and PHP 5.3.2 as well as our test server running Windows Server 2008, IIS7 and PHP 5.3.2

Test script:
---------------
<?php

class ExtendedException extends Exception {
    public function __construct($message, $code) {
        parent::__construct($message, $code);
        
        Log::Write("Hello!");
    }
}

class Log {
    // Note this method is not marked static
    public function Write($string) {
        print_r($this);
    }
}

try {
    throw new ExtendedException();
} catch (Exception $e) { }

?>


Expected result:
----------------
 

Actual result:
--------------
ExtendedException Object
(
    [message:protected] => 
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => D:\Public\Innovirt\bugtest.php
    [line:protected] => 18
    [trace:Exception:private] => Array
        (
        )

    [previous:Exception:private] => 
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-02 12:51 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-04-02 12:51 UTC] johannes@php.net
This is due to compatibility to a flaw of PHP 4. You have to mark your method explicitly static to prevent that:

    public static function Write($string) {
    ....
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 01 08:00:02 2025 UTC