php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #20760 strange behaviour regarding $this
Submitted: 2002-12-02 04:24 UTC Modified: 2004-09-18 11:11 UTC
From: krennmair at webdynamite dot com Assigned: didou (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.2.2 OS: RedHat 8.0
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: krennmair at webdynamite dot com
New email:
PHP Version: OS:

 

 [2002-12-02 04:24 UTC] krennmair at webdynamite dot com
We have found a strange behaviour regarding $this. One of our programmers made a mistake during programming, which led to "Heisenbugs", which were not quite easy to find and fix. We could reduce the problem to a simple program to present it:

<?

class Foo {

  var $bla;

  function quux() {
    $this->bla = 5;
  }

}

class Bar {

  var $bla;

  function do_stuff() {
    $this->bla = 10;
    Foo::quux();
    echo $this->bla;
  }
}

$blabla = new Bar;

$blabla->do_stuff();

?>

The output is: "5"

Obviously, Bar::do_stuff() is not allowed to call Foo::quux() since Foo::quux() is using $this. Now the strange thing comes: instead of casting an error, PHP happily accepts the code. But the $this in Foo::quux is the same $this as in Bar::do_stuff(), i.e. $blabla, and that's why the output is 5. Is this behaviour intended? At least I couldn't find it documented anywhere. IMO the user should be warned when $this is used in a static function.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-02 04:29 UTC] derick@php.net
This is indeed intended behavior, but it should be documented. I have some logs about this, but can not find them now.

Derick
 [2002-12-02 05:17 UTC] krennmair at webdynamite dot com
OK, but mark it in the documentation with a big red callsign. ;-)

But what I'm still interested in, is _why_ this is intended. The only reason I can think of is to confuse other programmers, but that would be absurd.
 [2002-12-03 21:06 UTC] wb at sapo dot pt
i once had made the same 'mistake': i had split up a inheritance chain at some place but then i needed a function of the other inheritance trtee after the split. in fact, it worked out fine - so fine that i even didn?t notice that the superclass call was outside the inheritance chain. when i later noticed it i removed it because i got worried if it was desired behaviour or if it could disappear in some later version of php. but if you tell me that i can rely on it i will use it.
 [2004-08-18 09:34 UTC] vrana@php.net
I think it's already documented at http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php :

"In this context, there is a current object and it may have object variables. Thus, when used from WITHIN an object function, you may use $this and object variables."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 19:01:28 2024 UTC