I'd like to do this:
<?php
define('X', 'attribute_name');
// object $thing is created with member attribute_name
echo $thing->X;
?>
Attempting $thing->X
, PHP takes X to be a property of $thing, and ignores the fact (rightly so) that it's a define()'d token. That in mind, I had expected $thing->{X}
to work, but no dice.
The only solution I'v come up with is to use a man-in-the-middle variable, like so:
$n = X;
echo $thing->$n;
But this extra step seems fairly un PHP-esque. Any advice on a more graceful solution?