子类中的PHP get

编程入门 行业动态 更新时间:2024-10-27 16:36:24
类中的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'); // false

How 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.

更多推荐

本文发布于:2023-07-27 14:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291486.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!