Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dynamic access constant - fork | making more promising and fail-safe
<?php
/* fork: making more promising and fail-safe */
class RegionVO {
const NSW = 35;
const ACT = 37;
const VIC = 38;
const QLD = 9;
const SA = 36;
const WA = 39;
const NT = 40;
const TAS = 26;
public function getID($alias) {
$refl = new ReflectionClass($this);
return $refl->getConstant(strtoupper($alias));
}
}
class RegionVO1 extends RegionVO {
const VIC = 48;
const NIU = 58;
}
$rvo = new RegionVO();
echo $rvo->getID('vic').PHP_EOL; // original
$rvo1 = new RegionVO1();
echo $rvo1->getID('vic').PHP_EOL; // overwritten
echo $rvo1->getID('nt').PHP_EOL; // inherited
echo $rvo1->getID('niu').PHP_EOL; // new constant
echo $rvo1->getId('abc'); // non-existent => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment