php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34990 static calls and $this
Submitted: 2005-10-26 11:11 UTC Modified: 2005-10-26 15:59 UTC
From: olympic at dino-online dot de Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.0.5 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-10-26 11:11 UTC] olympic at dino-online dot de
Description:
------------
In static calls $this is set. 

While in theory it might make sense to allow objects 
of the same class (or derived class) to "use" this 
feature, it's a potential pitfall and from my point 
of view it's bug.

I think if a non static method is called statically
then $this can't be any object that hasn't been
created by this or a derive class.

No other language has such a "feature" :)

If it's not a bug, then read my bugreport as 
"remove this feature". 
If it's a bug, add a runtime checking for $this is object
created by same or derived class.


Reproduce code:
---------------
Example:
Noone can really say that this is "transparen" ok:

class Foo {
        private    $hallo="I am from Foo";
        function dontCallMe() {

                return $this->hallo;
        }
}
class Bar {
        public $hallo="I am from Bar";
        function testMe() {
                return Foo::dontCallMe();
        }
}
$x=new Bar();
echo ($x->testMe()); // expected runtime error...









Expected result:
----------------
Runtime error.

Actual result:
--------------
output is: "I am from Bar"


This return the member var of Bar, what you would NEVER expect.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-26 11:21 UTC] derick@php.net
We will not change this behavior in the PHP 4 and 5 versions as it will break scripts. It's on the list for PHP 6.
 [2005-10-26 11:22 UTC] tony2001@php.net
This is done in order to keep backward compatibility with PHP4 and you can "fix" it easily declaring static methods as static (which is logically right). Methods declared as static do not have access to $this.
Most likely this will be changed in PHP6, but we didn't discuss it yet.
 [2005-10-26 15:59 UTC] olympic at dino-online dot de
ok, my example is missing:

error_reporting(E_ALL);

You don't get a warning, and the suggested "fix" does
of course not help - the method shouldn't be static in
my example:)


waiting for 6.0 .

However: I don't think that my suggestion to do a typecheck does break existing code (except buggy one).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 13:01:29 2024 UTC