php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34804 More information about static class methods
Submitted: 2005-10-10 10:12 UTC Modified: 2016-10-16 12:11 UTC
Votes:65
Avg. Score:4.8 ± 0.7
Reproduced:61 of 62 (98.4%)
Same Version:41 (67.2%)
Same OS:40 (65.6%)
From: toomuchphp-phpbugs at yahoo dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.x OS: Any
Private report: No CVE-ID: None
 [2005-10-10 10:12 UTC] toomuchphp-phpbugs at yahoo dot com
Description:
------------
Inside any class method, there are as many as 3 related class and it would be useful to have all of them as magic constants, and also in the debug_backtrace function.

Currently only __CLASS__ is available (the name of the direct owner of the method).  It would be good to know the name of the child class (if the method was called as part of a child class), and the name of the calling class if the method was called statically (the same as get_class($this) inside a static method).

This information would be most useful in the debug_backtrace() array.  debug_backtrace() was recently modified to report the direct owner class name rather than the inheriting class' name (see bug #30828) but it would really be more helpful in debugging to have all three possible class names available.

Reproduce code:
---------------
class A {
  function A() {
    echo 'direct owner: '.__CLASS__."\n";
    echo 'called as part of: '.__INHERITED_BY__."\n";
    echo 'called by instance of: '.__STATIC_CALLER__."\n";
  }
}

class B extends A { }

class C {
  function __construct() {
    B::A();
  }
}
new C();


Expected result:
----------------
direct owner: A
called as part of: B
called by instance of: C


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-11 21:35 UTC] kvesteri at cc dot hut dot fi
There should definetly be atleast constant __INHERITED_BY__. This new constant would allow many wonderful things like a generic singleton class and an efficient implementation of the martin fowler's Active Record pattern.
 [2006-06-01 02:33 UTC] toomuchphp-phpbugs at yahoo dot com
So ... now that I have learned C and played around with the 5.1.4 source for a couple of days, I've discovered it's not really possible to get information about the calling context using matic constants.

In which case, a function named something like 'get_static_child()' would suffice.  I am still trying to figure out how to find the name of the child class in the Zend hive of structs (if it is actually stored somewhere). :-S
 [2006-06-19 20:41 UTC] DouglasStewart at creighton dot edu
I am unsure where to comment on this. This particular issue that folks are having with __CLASS__ and self not playing in the proper calling context makes life very difficult. The bug report responses like "This is not a bug" and "is designed and intended to work that way" are, I suppose, the discretion of the current developer base or ZEND or whoever.

Maybe if someone could elaborate on the wisdom of this design decision. Imparting this knowledge may illuminate the reasons why this feature that many OO programmers are accustomed to having available is being dismissed so readily.

The current implementation of __CLASS__ is not useful.

If you wish to ask a class for its name, php is not yet prepared to provide you with the answer. The following examples illustrate why:

class TopClass {

	public static function ClassNameIndirect() {
		return self::ClassName();
	}
	public static function ClassName() {
		return 'TopClass';
	}
	public static function ClassNameKeyword() {
		return __CLASS__;
	}
	public static function ClassNameKeywordUninherited() {
		return self::ClassNameKeyword();
	}
	public static function ClassNameSelfInstance() {
		$object = new self;
		return get_class($object);
	}
	public static function ClassNameSelfInstanceUninherited() {
		return self::ClassNameSelfInstance();
	}
	public static function ClassNameKeywordUninheritedIndirect() {
		return self::ClassNameKeywordUninherited();
	}
	public static function ClassNameSelfInstanceUninheritedIndirect() {
		return self::ClassNameSelfInstanceUninherited();
	}
	public static function ClassNameHack() {
		$bt = debug_backtrace();
		$name = $bt[0]['class'];
		return $name;
	}
	public static function ClassNameHackUninherited() {
  	return self::ClassNameHack();
	}
}

class BottomClass extends TopClass {

	public static function ClassName() {
		return 'BottomClass';
	}
	public static function ClassNameKeywordUninherited() {
		return __CLASS__;
	}
	public static function ClassNameSelfInstanceUninherited() {
		$object = new self;
		return get_class($object);
	}
	public static function ClassNameHackUninherited() {
		$bt = debug_backtrace();
		$name = $bt[0]['class'];
		return $name;
	}
}

echo '<br />(1)*** Results for a static and constant class name implementation ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameIndirect();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameIndirect();
echo '<br />';
echo '<br />(2)*** Results for an inherited __CLASS__ implementation ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameKeyword();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameKeyword();
echo '<br />';
echo '<br />(3)*** Results for an UNinherited __CLASS__ implementation ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameKeywordUninherited();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameKeywordUninherited();
echo '<br />';
echo '<br />(4)*** Results for an Instance generated class name ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameSelfInstance();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameSelfInstance();
echo '<br />';
echo '<br />(5)*** Results for an UNinherited Instance generated class name ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameSelfInstanceUninherited();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameSelfInstanceUninherited();
echo '<br />';
echo '<br />(6)*** Results for an inherited hack to get class name ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameHack();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameHack();
echo '<br />';
echo '<br />(7)*** Results for an UNinherited hack to get class name ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameHackUninherited();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameHackUninherited();
echo '<br />';
echo '<br /> ---';
echo '<br />';
echo '<br />(8)*** #3 above with inherited method that calls the UNinherited __CLASS__ implementation ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameKeywordUninheritedIndirect();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameKeywordUninheritedIndirect();
echo '<br />';
echo '<br />(9)*** #5 above with inherited method that calls the UNinherited Instance generated class name ***';
echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameSelfInstanceUninheritedIndirect();
echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameSelfInstanceUninheritedIndirect();

// **
// ** Results
// **

(1)*** Results for a static and constant class name implementation ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: TopClass

(2)*** Results for an inherited __CLASS__ implementation ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: TopClass

(3)*** Results for an UNinherited __CLASS__ implementation ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: BottomClass

(4)*** Results for an Instance generated class name ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: TopClass

(5)*** Results for an UNinherited Instance generated class name ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: BottomClass

(6)*** Results for an inherited hack to get class name ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: TopClass

(7)*** Results for an UNinherited hack to get class name ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: BottomClass

---

(8)*** #3 above with inherited method that calls the UNinherited __CLASS__ implementation ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: TopClass

(9)*** #5 above with inherited method that calls the UNinherited Instance generated class name ***
ClassName() for TopClass: TopClass
ClassName() for BottomClass: TopClass

--

Examples #3 & #5 appear, at first, to work correctly, but after further testing (see #8 & #9), it becomes clear that the answer to the request for class name on the static (class) side of things is an exercise in futility.
 [2006-06-19 23:44 UTC] toomuchphp-phpbugs at yahoo dot com
The reason why some of the bug reports get dismissed so easily is because they ask for one of the existing features ('self' or '__CLASS__') to be modified to solve this problem, but 'self' and '__CLASS__' are both already very useful and proably very widely used, so it's not good to change them, and therefore we need a *new way* to find out what class the static method was part of.

Something I find very disappointing is the fact that bug #30828 destroyed the only existing solution to this problem - debug_backtrace().  In PHP 5.0.4, debug_backtrace() could have been used here to discover the static class name, but somebody wanted it working the other way (like __CLASS__ instead), the bug was reported, fixed in 5.0.5, and so we lost our only solution to this problem.  This bugfix actually broke some of my existing code a year ago when it was released, but it wasn't so important to me at the time.  It's only over the past year as I've been writing object-oriented code full time, and studying implementations by other languages (Java, AspectJ, Ruby) that I've realized how damaging the change to debug_backtrace() was.

I am hoping that as Zend continue writing their Framework, they'll realize just how broken static methods are and will try to fix them somehow.
 [2006-06-24 23:20 UTC] DouglasStewart at creighton dot edu
Understood toomuch, thanks.

I would then request the following:

Please provide the following on the static side:

$this -- in the context of a class side(static) method. $this represents an instance of ReflectionClass with all methods answering correctly as to the context of the call. So, for instance, statically implemented "$this->fileName()" would return the fileName that the current context class was loaded from. With this feature in place, I am confident that self will no longer be used for anything except in a root class like Object.

$sender -- Either a ReflectionClass instance when the call is made from a class-side implementation or a regular object instance.

class() -- An instance method that all objects inherit, that returns an instance of ReflectionClass for the class of the object context making the call.

Smalltalk offers some rather elegant solutions to these true OO problems that PHP's sudo-OO implementation struggles with.
 [2006-08-02 17:01 UTC] benjaminhill at gmail dot com
even something as simple as...

class Parent {
  static function myCurrentClassName() {
    return (__CALLINGCLASS__);
  }
}

class Child extends Parent {
  // empty
}

echo Child::myCurrentClassName() 
returns "Child"
 [2007-07-13 20:57 UTC] public1 at fridaythe14th dot com
I agree that there needs to be some method of accessing the calling class. The current implementation doesn't make sense and has no real world use. This is the thing that currently annoys me the most with PHP.

I don't really care how this gets solved. If self, __CLASS__ and get_class() are sacred any new constant or function would be appreciated.
 [2011-07-14 09:33 UTC] nospam at attractive-nuisance dot net
__STATIC__
 [2016-10-16 12:11 UTC] nikic@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: nikic
 [2016-10-16 12:11 UTC] nikic@php.net
This should be resolved as of PHP 5.3 with get_called_class() or as of PHP 5.5 with static::class.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 02:01:32 2025 UTC