php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33361 $this in static context
Submitted: 2005-06-16 13:14 UTC Modified: 2005-06-16 15:21 UTC
Votes:4
Avg. Score:4.0 ± 1.7
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: halmai at nexum dot hu Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.3.10 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: halmai at nexum dot hu
New email:
PHP Version: OS:

 

 [2005-06-16 13:14 UTC] halmai at nexum dot hu
Description:
------------
I have two classes with one function respectively. 

In any function the variable $this should mean 
- the instance of the class if this function was called as a function of an instance of _that_ class, and 
- it should be undefined (null) if called as a static function.

My problem is that if I call a static function of a class A from a non-static function of class B then in the member function of class A the variable $this denotes the instance of the class B instead of being null.

The fact that in a function of class A the variable $this denotes an instance of class B is really confusing. If it was called statically then $this should be null. 

I know that this is described as normal behaviour but I think this is a useless and senseless feature. In case of accepting my suggestion it would be easy to decide whether a function was called in a static or non-static context.

Reproduce code:
---------------
class A {
  function what_am_i() {
    if ( $this === null ) {
      print "i am not an instance<br>";
    } else {
      print "i am an instance from the class '".get_class( $this )."'.<br>";
    }
  }
}
class B {
  function wrapper() {
    A::what_am_i();  
  }
}
A::what_am_i(); // i am not an instance (this is OK)
$a = new A();
$a -> what_am_i(); // i am an instance from the class 'a'. (this is OK)

$b = new B();
$b -> wrapper();  // i am an instance from the class 'b'. (this should output not an instance)


Expected result:
----------------
i am not an instance
i am an instance from the class 'a'.
i am not an instance


Actual result:
--------------
i am not an instance
i am an instance from the class 'a'.
i am an instance from the class 'b'.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-16 13:28 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

. 
 [2005-06-16 13:39 UTC] halmai at nexum dot hu
I know that this is not a bug. This is a feature, but a really silly and useless feature.

That's why I reported it as a "feature/change request".
 [2005-06-16 13:52 UTC] halmai at nexum dot hu
Sorry, I forgot to reopen the state of this report.
 [2005-06-16 13:57 UTC] tony2001@php.net
As this is not a bug, it won't be changed in 4.3/4.4, because this is a bugfix-only branch.
 [2005-06-16 14:02 UTC] halmai at nexum dot hu
OK, I unsderstand.

Should I re-report this for version 5. or is it possible somehow to modify the php-version of this report? 

I could simply overwrite the version in the form but I can not reopen the report because I have only the "Wontfix" and "Closed" options.
 [2005-06-16 14:08 UTC] tony2001@php.net
>Should I re-report this for version 5.
Why? It works perfectly there.
 [2005-06-16 14:33 UTC] halmai at nexum dot hu
No, it is not perfect in version 5 neither. 

It works the same way in PHP 5.0.4 as in PHP 4.3.10.

So, should I re-report it or can You assign this report to a newer PHP version? If You can, then please do it. Thanks in advance.
 [2005-06-16 14:39 UTC] tony2001@php.net
It works fine if you declare the method as static.
If you don't do it - it works in the same way as in PHP4 just because of backward compatibility.
 [2005-06-16 14:58 UTC] halmai at nexum dot hu
In php5 when I use the keyword static then the value of the variable $this will always be null in this function. It's the correct behaviour because a static function can be called only without any instance. 

But if I want to call a function sometimes as an instance function and sometimes without any instances, then I have to omit the keyword static. But in this case I get the silly behaviour of php4, where I can not decide inside a function if it was called in static context or not. 

That's why I think this behaviour should be changed.
 [2005-06-16 15:14 UTC] tony2001@php.net
As I have already said: it won't be changed in PHP4 and it won't be changed in PHP5 because of BC.
 [2005-06-16 15:21 UTC] halmai at nexum dot hu
OK, I understand that it will not be changed in PHP4.

If BC stands for backword compatibility, then I do not agree. 

I think this "feature" was not used by too many users but many of them were surprised by this behavior (see earlier bugreports). That's why it should be changed, I think. Maybe in PHP6?

If a feature is senseless and embarrasses some users and any others are not using this feature at all then it could be changed, I think.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 12:01:29 2024 UTC