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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cmanley at office dot caiw dot nl
New email:
PHP Version: OS:

 

 [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: Thu May 02 04:01:30 2024 UTC