Doc #52427 [Com]: $this is used from the caller scope if the called method is static

From: Date: Sun, 15 Aug 2010 08:18:56 +0000
Subject: Doc #52427 [Com]: $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-4863@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52427&edit=1 ID: 52427 Comment by: tyra3l at gmail dot com Reported by: tyra3l at gmail dot com Summary: $this is used from the caller scope if the called method is static Status: Feedback Type: Documentation Problem Package: Class/Object related Operating System: all PHP Version: 5.3.3 Block user comment: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [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 http://bugs.php.net/bug.php?id=52427&edit=1

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