Getting the name of a child class in the parent class (static context)

前端 未结 9 944
栀梦
栀梦 2020-11-29 20:19

I\'m building an ORM library with reuse and simplicity in mind; everything goes fine except that I got stuck by a stupid inheritance limitation. Please consider the code bel

9条回答
  •  伪装坚强ぢ
    2020-11-29 20:31

    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"
    

提交回复
热议问题