Is anyone familiar with working with this library: https://github.com/eloquent/enumeration
I am having trouble converting the instance of the constant back to the constant value.
class TestEnum extends AbstractEnumeration
{
const THING1 = 'test1';
const THING2 = 'test2';
}
class DoStuff
{
public function action(TestEnum $test)
{
if($test === 'test1') {
echo 'THIS WORKS';
}
}
}
$enumTest = TestEnum::THING1();
$doStuff = new DoStuff();
$doStuff->action($enumTest);
My goal is to have the method action print 'THIS WORKS'. Because $test is an instance of TestEnum, this would not evaluate to true.
You're close, but there are two problems:
Thing1 != THING1$test, when treated as a string, evaluates to its key THING1. You want its value $test->value()class TestEnum extends AbstractEnumeration
{
const THING1 = 'test1';
const THING2 = 'test2';
}
class DoStuff
{
public function action(TestEnum $test)
{
if($test->value() === 'test1') {
echo 'THIS WORKS';
}
}
}
$enumTest = TestEnum::THING1();
$doStuff = new DoStuff();
$doStuff->action($enumTest);
THIS WORKS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With