In general, the PHP provides a function that takes a JSON string that usually represents a javascript array or object literals for converting this encoded string in JSON format to a particular PHP object data type. It's still worth mentioning though.In PHP the inbuilt function json_decode() function is defined as a function as it name suggest it is a function for conversion of any JSON a standard text format objects or decoding of JSON objects to PHP objects, taking a JSON string as a parameter which this function converts or decodes it and returns the encoded values which were in JSON into proper PHP objects. One last thing: enums also have a read only property $enum-> name, which the RFC mentions is an implementation detail and should probably only be used for debugging purposes. Note that TARGET_CLASS filter will also include enums. Just like normal classes and properties, enums and their cases can be annotated using attributes. There's also a new enum_exists function which does what its name suggests. # Reflection and attributesĪs expected, there are a few reflection classes added for dealing with enums: ReflectionEnum, ReflectionEnumUnitCase and ReflectionEnumBackedCase. You're not allowed to override built-in enum methods, and they can't contain class properties - those are prohibited on enums. # TraitsĮnums can use traits just like classes, but with some more restrictions. This means you can only use enums as keys in SplObjectStorage and WeakMaps. There is an RFC to change this behaviour, but it hasn't been voted yet. The following will result in an error: $list = [ $statusC instanceof Status // true # Enums as array keysīecause enums values are actually objects, it's currently not possible to use them as array keys. That means that you can do comparisons with them like so: $statusA = Status:: PENDING I already mentioned that enums values are represented as objects, in fact those are singleton objects. Note that this array contains the actual enum objects: array_map(įn( Status $status) => $status-> color(), You can use the static Enum:: cases() method to get a list of all available cases within an enum: Status:: cases() This behaviour can be overridden by implementing JsonSerializable. Furthermore, you can use json_encode in combination with backed enums, its result will be the enum value. Note that you can also use the built-in serialize and unserialize functions on enums. $status = Status:: tryFrom( 'unknown') // null $status = Status:: from( 'unknown') // ValueError If you'd use from there would be an exception. There's also a tryFrom that returns null if an unknown value is passed. Restoring an enum from a value can be done by using Enum:: from: $status = Status:: from( 2) // Status::PUBLISHED That's done with a readonly public property: $value = Status:: PUBLISHED-> value // 2 Serializing them means you need a way to access the enum's value. If you're assigning values to enum cases, you probably want a way to serialize and deserialize them. If you're combining backed enums and interface, the enum type must come directly after the enum name, before the implements keyword. Enums that aren't "backed" are called "pure enums". If you decide to assign enum values, all cases should have a value. The technical term for typed enums is called "backed enums" since they are "backed" by a simpler value. Take note that only int and string are allowed as enum values. It indicates that all enum values are of a given type. Note the type declaration in the enum definition. The benefit of enums is that they represent a collection of constant values, but most importantly those values can be typed, like so: class BlogPostĮnum values are represented by objects internally, but you can assign a value to them if you want to this is useful for eg. As usual with my PHP feature posts, we start with a high level overview of what enums look like: enum Status
0 Comments
Leave a Reply. |