#20760 [Opn]: strange behaviour regarding $this

From: Date: Mon, 02 Dec 2002 11:17:05 +0000
Subject: #20760 [Opn]: strange behaviour regarding $this
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969350096@lists.php.net to get a copy of this message
ID: 20760 User updated by: krennmair@webdynamite.com Reported By: krennmair@webdynamite.com Status: Open Bug Type: Documentation problem Operating System: RedHat 8.0 PHP Version: 4.2.2 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2002-12-02 04:29:18] 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 04:24:24] krennmair@webdynamite.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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=20760&edit=1

« previous php.doc (#969350096) next »