dqbhdsec59405 2011-10-29 04:20
浏览 262
已采纳

从PHP中的父级获取子类的常量列表

so suppose I have

class B extends A{}

and B contains a whole bunch of class constants

Is there a way to define a static function in A that would fetch all class constants in the calling child class?

So for example is there a way to define static function getChildConstants() in A

such that if you call B::getChildConstants() (B can access this function since B extends A), this function would return all the class constants in B. Moreover, this function must be defined in A only. ie, I don't want to have to reimplement getChildCosntants() in B.

This is so that for any class that extends A, I can easily get the constants of such class without having to reimplement/rewrite/copy and paste getChildConstants() into chose children classes over and over again

so I want a universal constants fetching function in A such that for any class that extends A, I can get the class constants of such class by simply calling this universal constants fetching function that was defined in A

I must reiterate that the getChildConstant() function must be static

  • 写回答

1条回答 默认 最新

  • dqwcdqs358367 2011-10-29 04:29
    关注

    You don't need to define a function for that. You can use reflection directly on child classes.

    $reflector = new ReflectionClass('B');
    var_dump($reflector->getConstants());
    

    Will this satisfy your needs?

    get all class constants

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?