![]() ![]() This is where json_encode (and json_decode) come into the picture and enable PHP processed data to be compatible with frameworks and systems that deal with JSON data and make way for easier and faster web development. Since JSON is a ubiquitous data format for sharing and storing data, it is vital that a PHP backend allows processing of JSON data. A decent backend framework like PHP needs to be capable of providing and processing data in any format (e.g., XML, JSON, etc.) to be socially accepted in a society of skilled web development frameworks. It's still worth mentioning though.PHP is a server-side scripting language for creating your website’s backend system that can serve webpages, communicate with databases, and exchange data over the internet. 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |