I'm not sure what the correct wording of my question is, but I added an enum
(NP_PostTypeType
) because I need to know what the type of each item in my initial enum (NP_PostType
) is.
I store the current PostType in $postType
which gets fed into a method and now in that method I need to extract the type for each type.
What I tried doing was: switch(NP_PostTypeType::$type)
, but this yields: Fatal error: Access to undeclared static property: NP_PostTypeType::$type
These are my 2 enums:
abstract class NP_PostType extends BasicEnum {
const Event = "event";
const Job = "job";
const Quote = "quote";
const Status = "status";
const Video = "video";
}
abstract class NP_PostTypeType extends BasicEnum {
const Event = "type";
const Job = "type";
const Quote = "format";
const Status = "format";
const Video = "format";
}
How do I go about this?
You can't access a constant like you do with a static property, the constant function is your solution :
$type = constant('NP_PostTypeType::' . $postType);
But be careful, your $postType
case must match your NP_PostTypeType
constant names (currently not the case), you should update your NP_PostType class to :
abstract class NP_PostType extends BasicEnum {
const Event = "Event";
const Job = "Job";
const Quote = "Quote";
const Status = "Status";
const Video = "Video";
}
Try converting expression to string: switch(constant("NP_PostTypeType::$type"))
Use constant,
$constant = "Event";
$classname = "NP_PostType";
$variable = $classname.'::'.$constant;
echo constant($variable); //event