You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
btw it throws error too when try to check array key (if index is not \ArrayAccess)
but you dont forced to try/catch + tryFrom.
only purpose is like "enum typehint", that instead of automatic process - forces colleagues to do things manually and then take responsibility about.
language feature that allow developers to drop off responsibility! what a beauty.
yes, valueobjects that means "valid objects" is a pattern, but wrapping plain strings/integers to the object its like "call 3 functions instead of one isset to be cool"
so, solution for case objects/strings/integers would be:
but for enums itself much bigger code type_struct_enum(&$result, $value, $flags) with flags like this, and this function should never check strings to be part of enum, just return Enum class if enum case object provided, or Enum class name provided.
if (! defined('_PHP_STRUCT_TYPE_CLASS')) define('_PHP_STRUCT_TYPE_CLASS', 1 << 1);
if (! defined('_PHP_STRUCT_TYPE_INTERFACE')) define('_PHP_STRUCT_TYPE_INTERFACE', 1 << 2);
if (! defined('_PHP_STRUCT_TYPE_TRAIT')) define('_PHP_STRUCT_TYPE_TRAIT', 1 << 3);
if (! defined('_PHP_STRUCT_TYPE_ENUM')) define('_PHP_STRUCT_TYPE_ENUM', 1 << 4);
if (! defined('_PHP_STRUCT_TYPE_ALL')) define('_PHP_STRUCT_TYPE_ALL', (1 << 5) - 1);
if (! defined('_PHP_STRUCT_EXISTS_TRUE')) define('_PHP_STRUCT_EXISTS_TRUE', 1 << 5);
if (! defined('_PHP_STRUCT_EXISTS_FALSE')) define('_PHP_STRUCT_EXISTS_FALSE', 1 << 6);
if (! defined('_PHP_STRUCT_EXISTS_IGNORE')) define('_PHP_STRUCT_EXISTS_IGNORE', 1 << 7);
and then check _PHP_STRUCT_EXISTS_IGNORE -> only regex
_PHP_STRUCT_EXISTS_TRUE -> only exists with flags TYPE
_PHP_STRUCT_EXISTS_FALSE -> exists, then regex...
The text was updated successfully, but these errors were encountered:
Enumerations are built on top of classes and objects.
We have made the very conscious decision to base enums on classes so that they can share a large amount of the existing architecture, like methods, interfaces, constants for cases, etc. PHP isn't the only language that does this either.
tryFrom() -> surround it to try/catch -> convert to string before it -> do it for any user input after validation.
tryFrom is intended to validate values of the correct type. Some argument could be made for accepting arbitrary values. However, this behavior was specified and voted on, so changing it requires an RFC.
var_dump(isset($index[ 'hello' ])); // true
but you dont forced to try/catch + tryFrom.
This is not true. The fair comparison would be this, which does throw.
BEST IMPLEMENTATION.
Please keep the snarky comments to yourself. Otherwise, I will stop engaging with your issues, as most others have already chosen to.
Description
The following code:
Resulted in this output:
But I expected this output instead:
PHP Version
PHP 8.4.5 (cli)
Operating System
https://onlinephp.io/
Really, there's strange world.
Bloggers hype that "FINALLY, ENUMS!!11", but actually
does not support strings, so i need to MyBackedStringEnum::tryFrom() that should not throw any exceptions instead of ::from()
but
I mean, you made Enums to escape array-like lists and calling isset() to check value, but nowadays you need to call
tryFrom() -> surround it to try/catch -> convert to string before it -> do it for any user input after validation.
BEST IMPLEMENTATION.
You know, this one works faster and looks smaller (combo x2):
btw it throws error too when try to check array key (if index is not \ArrayAccess)
but you dont forced to try/catch + tryFrom.
only purpose is like "enum typehint", that instead of automatic process - forces colleagues to do things manually and then take responsibility about.
language feature that allow developers to drop off responsibility! what a beauty.
yes, valueobjects that means "valid objects" is a pattern, but wrapping plain strings/integers to the object its like "call 3 functions instead of one isset to be cool"
so, solution for
case objects/strings/integers
would be:but for enums itself much bigger code
type_struct_enum(&$result, $value, $flags)
withflags
like this, and this function should never check strings to be part of enum, just returnEnum class
ifenum case object
provided, orEnum class name
provided.and then check _PHP_STRUCT_EXISTS_IGNORE -> only regex
_PHP_STRUCT_EXISTS_TRUE -> only exists with flags TYPE
_PHP_STRUCT_EXISTS_FALSE -> exists, then regex...
The text was updated successfully, but these errors were encountered: