I'm using my own way of enums since older versions of PHP doesn't support them. My implementation of enums:
class ModeOfTransport{
const Land = 0;
const Naval= 1;
const Air= 2;
function GetClassVars()
{
return array_keys(get_class_vars(get_class($this))); // $this
}
static function getConstants() {
$oClass = new ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
static function getValues(){
return ModeOfTransport::getConstants();
}
static function getDisplayText(int $val){
$key=array_search($val,ModeOfTransport::getConstants());
return $key;
}
}
And since I'm having many other enums which feature also the same methods "getConstants()" etc. So i thought instead of repeating the same methods in every single class i can just use inheritance to apply it to all of them. But it didn't work as i expected. When i try:
class CustomEnum
{
const aa="test";
public static function getConstants() {
$oClass = new ReflectionClass(self::class);
return $oClass->getConstants();
}
public static function getValues(){
return self::getConstants();
}
public static function getDisplayText(int $val){
$key=array_search($val,ModeOfTransport::getConstants());
return $key;
}
}
class DocumentMainType extends CustomEnum {
const VehicleDocument = 0;
const DriverDocument= 1;
}
//And calling the related method:
$var=DocumentMainType::getConstants();
//$var is: {"aa":"test"}
//Expected $var: {"VehicleDocument":0,"DriverDocument":1}
How can i get it to work so that those classes who inherit CustomEnum class can easily reflect their variables through inherited getConstants() method.
Thank you
Aucun commentaire:
Enregistrer un commentaire