Skip to content

Instantly share code, notes, and snippets.

@adhocore
Forked from samundra/constant access.php
Last active December 30, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adhocore/7787225 to your computer and use it in GitHub Desktop.
Save adhocore/7787225 to your computer and use it in GitHub Desktop.
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