Getting the name of a child class in the parent class (static context)
in short. this is not possible. in php4 you could implement a terrible hack (examine the debug_backtrace()
) but that method does not work in PHP5. references:
30423
37684
34421
edit: an example of late static binding in PHP 5.3 (mentioned in comments). note there are potential problems in it's current implementation (src).
class Base {
public static function whoAmI() {
return get_called_class();
}
}
class User extends Base {}
print Base::whoAmI(); // prints "Base"
print User::whoAmI(); // prints "User"
Getting the name of a class on which a static function has been called
The following code should work:
class Base { static getClassName() { return this.name; }}
class Extended extends Base {}
console.log(Base.getClassName()); // Baseconsole.log(Extended.getClassName()); // Extended
PHP Get name of child class in static function
<?php
class B extends A
{
public static function fun()
{
parent::fun();
}
}
class A
{
public static function fun()
{
var_dump(get_called_class());
}
}
B::fun();
http://php.net/manual/en/function.get-called-class.php
Get child class name from parent with _CLASS_
__CLASS__
is "compile" time constant, this is why it will be replaced with "Parent" at the very start of script execution Magic constants
this constant returns the class name as it was declared
to get current class name you can use get_class function
Get test class name within static context from it's parent
I found the solution using '@ClassRule' and implemting a customg TestWatcher
. As the starting(Description description)
is called before the @BeforeClass public static void beforeClass()
method I can use it like this:
Here is my code:
public abstract class BaseTest {
@ClassRule
public static CustomTestWatcher classWatcher = new CustomTestWatcher();
private static Map<String, String> testConfig;
private static FakeApplication fakeApplication;
public static class CustomTestWatcher extends TestWatcher {
private String className = BaseTest.class.getSimpleName();
public String getClassName() {
return className;
}
private void setClassName(String className) {
this.className = className;
}
@Override
protected void starting(Description description) {
//This will set className to = FooTest!
setClassName(description.getClassName());
System.out.println("\nStarting test class: " + className);
}
}
@BeforeClass
public static void beforeClass() {
//Here now finally is FooTest!
String className = classWatcher.getClassName();
System.out.println("Creating Test Database and Fake Application for " + className);
...
}
Java: get the class of the inherited class from a static method
That's not the way that static methods work. You'll have to implement Derived.foo()
, do whatever it is that's special to Derived
, and that method then calls Base.foo()
. If you really need the type information, you could create Base.foo0(Class klass)
.
But to be honest, any static method that needs to know that type of the class that it's invoked on should probably be an instance method.
Related Topics
Codeigniter Sessions Not Working After Migration
Calculate the Number of Months Between Two Dates in PHP
Setting a PHP Script as a Windows Service
How to Skip Invalid Characters in Xml File Using PHP
How to Define a Laravel Route with a Parameter That Contains a Slash Character
Foursquare API for Venue User Image Error
Downloading a File with a Different Name to the Stored Name
Can't Decrypt Using Pgcrypto from Aes-256-Cbc But Aes-128-Cbc Is Ok
Composer Require Local Package
How to Access a Property of an Object (Stdclass Object) Member/Element of an Array
Using Wget to Run a Cronjob PHP
Issue in Installing PHP7.2-Mcrypt
How to Run Laravel Without Artisan
Easiest Way to Alternate Row Colors in PHP/Html
Reflectionexception: Class Classname Does Not Exist - Laravel