|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52969 This defined in static call from within another class
Submitted: 2010-10-02 15:24 UTC Modified: 2010-10-04 20:07 UTC
Avg. Score:2.8 ± 1.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: ircmaxell at yahoo dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.3 OS: linux
Private report: No CVE-ID: None
 [2010-10-02 15:24 UTC] ircmaxell at yahoo dot com
When calling a non-static method statically from within another class (non-
inherited), $this is populated with the other object's class.

Test script:
class test1 {
    public function getName() {
        return $this->name;
class test2 {
    public $name = 'foo';
    public function getName() {
        return test1::getName();
$obj = new Test2;
echo $obj->getName();

Expected result:
Fatal Error: Using $this when not in object context

Actual result:
"foo" is outputted.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-02 15:28 UTC] gnuffo1 at gmail dot com
This seems to have existed way back in PHP 4.1:

If this is a bug, then I think it should be turned into a feature and documented. It is occasionally useful for trait-like functionality.
 [2010-10-02 15:33 UTC] ircmaxell at yahoo dot com
Oh, and $this is an object of class Test2 within Test1::getName()...
 [2010-10-02 15:33 UTC] php at rwasmus dot nl
Can reproduce across architectures & versions from 5.2.9 onward, IMHO opinion, just an E_STRICT error in between is understating the scripters design flaw. Some existing code may rely on it, so a real fix may hurt some packages, but at the very least I'd increase the errors severity.
 [2010-10-02 15:51 UTC] gnuffo1 at gmail dot com
It has been pointed out to me that this is actually documented:

Under example 2 and specifically the lines:

$b = new B();
 [2010-10-02 20:07 UTC]
-Status: Open +Status: Bogus
 [2010-10-02 20:07 UTC]
Closing as bogus. As you have pointed out, this is documented.
 [2010-10-02 21:05 UTC] ircmaxell at yahoo dot com
Close it if you wish, but I do honestly believe this is a major bug...

This means that you cannot trust that $this is an instance of the class it's 
used in.  So does that mean that we must add:

($this instanceof self) or throw new Exception('called from another class');

to all of our methods?  

Remember, any sufficiently advanced bug is indistinguishable from a feature (no 
matter if it is documented or not).  But don't be afraid to call it what it 
really is...  Just because it's documented, doesn't mean it isn't a bug...

Again, IMHO...
 [2010-10-03 00:37 UTC] gnuffo1 at gmail dot com
Does it really mean you must put that line in every method? It's not like you ever accidentally had a problem with it before is it?
 [2010-10-03 18:28 UTC] + at ni-po dot com
@ircmaxell: No, you don't need to modify all your code and add that line. Or at least you don't need to do so if you don't already have this line in every method:
if (!isset($this)) throw new LogicException('Called non-static method statically');
In PHP you can't rely on $this being set. But still you do, because an unset $this is a real edge case.

But I still do think that this feature *is* a bug and shall be removed. It is absolutely illogical that $this isn't instanceof self.
 [2010-10-04 19:43 UTC] jesse_7254 at yahoo dot com
A bogus bug is a bogus bug, no matter the opinion of StackOverflow amateurs.
 [2010-10-04 20:07 UTC]
Yes, perhaps this feature ought to be removed, but the place to discuss it is the internals mailing list.

In any case, it certainly wouldn't be removed from PHP 5.3, only trunk, as it would be a breaking change.
 [2010-10-08 22:55 UTC] + at ni-po dot com
@jesse: That comment was completely unnecessary.

@all: I couldn't find any documentation on what a bogus bug is. Could somebody tell (or link)? Thanks.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC