How to Get the Classname from a Static Call in an Extended PHP Class

How can I get the classname from a static call in an extended PHP class?

__CLASS__ always returns the name of the class in which it was used, so it's not much help with a static method. If the method wasn't static you could simply use get_class($this). e.g.

class Action {
public function n(){
echo get_class($this);
}

}

class MyAction extends Action {

}

$foo=new MyAction;

$foo->n(); //displays 'MyAction'

Late static bindings, available in PHP 5.3+

Now that PHP 5.3 is released, you can use late static bindings, which let you resolve the target class for a static method call at runtime rather than when it is defined.

While the feature does not introduce a new magic constant to tell you the classname you were called through, it does provide a new function, get_called_class() which can tell you the name of the class a static method was called in. Here's an example:

Class Action {
public static function n() {
return get_called_class();
}
}


class MyAction extends Action {

}


echo MyAction::n(); //displays MyAction

how call class name from extend class

You can do that with get_called_class, although it's not clear why you would want to make it a method.

Get child class name php from static function

Instead of get_class(), use get_called_class().

How do I get class name in PHP?

Since PHP 5.5 you can use class name resolution via ClassName::class.

See new features of PHP5.5.

<?php

namespace Name\Space;

class ClassName {}

echo ClassName::class;

?>

If you want to use this feature in your class method use static::class:

<?php

namespace Name\Space;

class ClassName {
/**
* @return string
*/
public function getNameOfClass()
{
return static::class;
}
}

$obj = new ClassName();
echo $obj->getNameOfClass();

?>

For older versions of PHP, you can use get_class().

Get class name from extended class

Use:

get_class($this);

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

How to get class name from static child method

__CLASS__ is a pseudo-constant, that always refers to the class, where it is defined. With late-static-binding the function get_called_class() were introduced, that resolve the classname during runtime.

class MyParent {

public static function tellSomething() {
return get_called_class();
}
}

class MyChild extends MyParent {

}

echo MyChild::tellSomething();

(as a sidenote: usually methods don't need to know the class on were they are called)

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"


Related Topics



Leave a reply



Submit