Doc #52427 [NEW]: $this is used from the caller scope if the called method is static
| From: | tyra3l at gmail dot com | Date: | Sat, 24 Jul 2010 08:34:07 +0000 |
| Subject: | Doc #52427 [NEW]: $this is used from the caller scope if the called method is static | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-4728@lists.php.net to get a copy of this message | ||
From:
Operating system: all
PHP version: 5.3.3
Package: Class/Object related
Bug Type: Documentation Problem
Bug description:$this is used from the caller scope if the called method is static
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 bug report at http://bugs.php.net/bug.php?id=52427&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52427&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52427&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52427&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=52427&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52427&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=52427&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=52427&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=52427&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=52427&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=52427&r=support
Expected behavior: http://bugs.php.net/fix.php?id=52427&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=52427&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=52427&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=52427&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52427&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52427&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=52427&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=52427&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=52427&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=52427&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=52427&r=mysqlcfg