ravinggenius Posted August 1, 2008 Share Posted August 1, 2008 <?php class Model { private static $type = 'Model'; public static function test1() { $c = __CLASS__; echo "found a {$c} object\n"; } public static function test2() { $c = self::$type; echo "found a {$c} object\n"; } } class User extends Model { private static $type = 'User'; } Model::test1(); Model::test2(); echo "\n"; User::test1(); User::test2(); ?> This results in the following: found a Model object found a Model object found a Model object found a Model object As you can see in the example above, when I am in the User class, I cannot get the correct class name. Does anybody know of a way to find the name of the class when I am in an extended static method? That is, calling User::test1(); or User::test2(); should result in 'found a User object'. Is this even possible? Link to comment https://forums.phpfreaks.com/topic/117760-extending-a-class-and-getting-the-class-name-statically/ Share on other sites More sharing options...
Daniel0 Posted August 1, 2008 Share Posted August 1, 2008 It's not possible. You'll have to use late static binding which is only in 5.3 and 6.0. Link to comment https://forums.phpfreaks.com/topic/117760-extending-a-class-and-getting-the-class-name-statically/#findComment-605725 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.