php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60202 Please add __STATIC__ magic constant
Submitted: 2011-11-02 14:20 UTC Modified: 2011-11-02 17:07 UTC
From: cmanley at office dot caiw dot nl Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
33 - 23 = ?
Subscribe to this entry?

 
 [2011-11-02 14:20 UTC] cmanley at office dot caiw dot nl
Description:
------------
Please add a __STATIC__ magic constant so that a static method can know the name of the class that it was called on.

In other words:
class A {
  static function bla() {
    print __STATIC__; 
  }
} 

class B extends A {}

print B::bla() . "\n"; // I want this to print "B".


See also:
http://www.php.net/manual/en/language.constants.predefined.php#84050

Test script:
---------------
// This is how it is currently done:
php -r 'class A { static function bla() { print get_called_class(); }} class B extends A {}  print B::bla() . "\n";'  
B

// This would be more consistent and convenient:
php -r 'class A { static function bla() { print __STATIC__; }} class B extends A {}  print B::bla() . "\n";'  
B



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-02 17:05 UTC] johannes@php.net
You can use the static keyword for that. See http://php.net/lsb
 [2011-11-02 17:05 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-11-02 17:07 UTC] johannes@php.net
Sorry, misread your request.

But a constant can't be done for technical reasons. The value of the magic constant has to be known at compile time, but the lsb name is only known at run time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 03:01:32 2024 UTC