2

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

hakre
  • 193,403
  • 52
  • 435
  • 836
kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171

1 Answers1

1

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

Community
  • 1
  • 1
Vikk
  • 3,353
  • 3
  • 22
  • 24