Doc #52427 [Fbk->NoF]: $this is used from the caller scope if the called method is static

From: Date: Mon, 18 Feb 2013 00:34:23 +0000
Subject: Doc #52427 [Fbk->NoF]: $this is used from the caller scope if the called method is static
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-9536@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52427&edit=1 ID: 52427 Updated by: php-bugs@lists.php.net Reported by: tyra3l at gmail dot com Summary: $this is used from the caller scope if the called method is static -Status: Feedback +Status: No Feedback Type: Documentation Problem Package: Class/Object related Operating System: all PHP Version: 5.3.3 New Comment: No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Open". Thank you. Previous Comments: ------------------------------------------------------------------------ [2010-08-15 10:18:56] tyra3l at gmail dot com http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php http://php.net/manual/en/language.oop5.static.php this was the first two place where I tried to find documentation about this behavior. Tyrael ------------------------------------------------------------------------ [2010-08-15 09:43:02] degeberg@php.net Where do you propose it should be documented instead? ------------------------------------------------------------------------ [2010-07-24 11:28:18] tyra3l at gmail dot com http://www.php.net/manual/en/language.oop5.basic.php "The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object)." So it is indeed documented, maybe not on the best place of all... ------------------------------------------------------------------------ [2010-07-24 10:34:06] tyra3l at gmail dot com Description: ------------ Samuel pointed out this "feature" on the php-windows mailing list: http://marc.info/?t=127981502400008&r=1&w=2 this "feature" bit many people (see the bug reports, the comments on language.oop5.static.php), so I think we should add this behaviour to the documentation: http://php.net/manual/en/language.oop5.static.php "Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static." "Calling non-static methods statically generates an E_STRICT level warning." We should add that the non-static methods can be called via the scope resolution operator (aka the static way) but in this case the method will have access to the caller method's scope and an E_STRICT warning will be raised. Test script: --------------- <?php class A{ public $foo = 'bar'; public function write(){ print($this->foo); } } class B{ public $foo = 'gnagnagna'; public function write(){ A::write(); } } $var = new B; $var->write(); Expected result: ---------------- PHP Fatal error: Using $this when not in object context in... OR Change in the documentation to describe this kind of behaviour. Actual result: -------------- gnagnagna ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=52427&edit=1

« previous php.doc.bugs (#9536) next »