We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
To do this, I had to expand Enum to support null values. It is also a good idea to support null as a separate enum value.
null
I recommend to use array_key_exist() instead of isset() for validate Enum value:
array_key_exist()
isset()
New:
public static function __callStatic($name, $arguments) { $array = static::toArray(); if (\array_key_exists($name, $array)) { return new static($array[$name]); } throw new \BadMethodCallException("No static method or enum constant '$name' in class " . \get_called_class()); }
Example:
<?php final class MyEnum extends \MyCLabs\Enum\Enum { public const UNDEFINED = null; }
I can put null to __construct()
__construct()
<?php /** @var string|int|null $value */ $enum = new MyEnum($value); // It can take null
But i cant do it with static:
<?php $enum = MyEnum::UNDEFINED(); // I'll catch an exception
And finally i can remove excess ? operator from methods and constructors:
?
This is look better
public function someMethod(MyEnum $enum) { if (!$enum->equals(MyEnum::UNDEFINED()) { // ... } }
then this
<?php public function someMethod(?MyEnum $enum) { if ($enum !== null) { // do ... } }
The text was updated successfully, but these errors were encountered:
Implementation: #78
Sorry, something went wrong.
You can combine isset and array_key_exists to get best of both worlds.
isset
array_key_exists
if (isset($array[$name]) || \array_key_exists($name, $array)) {
isset is a language construct and a bit faster than array_key_exists.
Yes, I agree. In this case, array_key_exists will be executed secondary if isset returns false
Really nice fix and thanks @willemstuursma too, merging!
enum
No branches or pull requests
To do this, I had to expand Enum to support
null
values.It is also a good idea to support null as a separate enum value.
I recommend to use
array_key_exist()
instead ofisset()
for validate Enum value:New:
Example:
I can put
null
to__construct()
But i cant do it with static:
And finally i can remove excess
?
operator from methods and constructors:This is look better
then this
The text was updated successfully, but these errors were encountered: