I am trying to iterate through variables in a PHP class that contains SplEnum. This does not work. Here is the code:
class enum extends SplEnum { const First = 1; }
class fruit
{
public $enum;
public $variable = 2;
public function __construct(enum $enum)
{
$this->enum = $enum;
}
}
$apple = new fruit(new enum(enum::First));
foreach ($apple as $key => $value) {
echo "[$key] => $value\n";
}
This is the conclusion:
[enum] => 1
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Value not a const in enum enum' in /home/test.php:16
Stack trace:
#0 /home/test.php(16): unknown()
#1 {main}
thrown in /home/test.php on line 16
It seems that what happens is that the loop is foreachtrying to turn every class variable into enum. How to iterate over variables in a class?
source
share