子类中的PHP get_class()功能(PHP get_class() functionality in child classes)
我需要检查一个属性是否存在,这是有效的:
class someClass { protected $some_var public static function checkProperty($property) { if(!property_exists(get_class()) ) { return true; } else return false; } }但是现在当我尝试扩展课程时,它不再起作用了。
class someChild extends someClass { protected $child_property; } someChild::checkProperty('child_property'); // false我如何获得我想要的功能? 我尝试用$this替换get_class() , self , static ,没什么用。
I need to check if a property exists and this works:
class someClass { protected $some_var public static function checkProperty($property) { if(!property_exists(get_class()) ) { return true; } else return false; } }But now when I try to extend the class, it doesn't work anymore.
class someChild extends someClass { protected $child_property; } someChild::checkProperty('child_property'); // falseHow do I get the functionality I want? I tried replacing get_class() with $this, self, static, nothing works.
最满意答案
我相信我找到了正确的答案。 对于静态方法,请使用get_called_class() 。
也许$this适用于对象方法。
I believe I've found the correct answer. For static methods, use get_called_class().
Perhaps $this works for object methods.
更多推荐
发布评论