-
-
Save adhocore/7787225 to your computer and use it in GitHub Desktop.
dynamic access constant - fork | making more promising and fail-safe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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