PHP does not have an enumerated type, so Slice enumerations are mapped to constants in a PHP class: the name of the Slice enumeration becomes the name of the PHP class; for each enumerator, the class contains a constant with the same name as the enumerator (see
Section 28.3 for more information on identifiers). For example:
Since enumerated values are mapped to integers, application code is not required to use the generated constants. When an enumerated value enters the Ice run time, Ice validates that the given integer is within the expected range for the enumeration. However, to minimize the potential for defects in your code, we recommend using the generated constants instead of literal integers.
A Slice structure maps to a PHP class containing a public variable for each member of the structure. The class also provides a constructor whose arguments correspond to the data members. This allows you to instantiate and initialize the class in a single statement (instead of having to first instantiate the class and then assign to its members). Each argument provides a default value appropriate for the member’s type. You can also declare different default values for members of primitive and enumerated types, as discussed in
Section 4.9.2. For example, here is our
Employee structure from
Section 4.9.4 once more:
The mapping includes a definition for the __toString magic method, which returns a string representation of the structure.
Slice sequences are mapped to native PHP indexed arrays. The first element of the Slice sequence is contained at index 0 (zero) of the PHP array, followed by the remaining elements in ascending index order.
The Ice run time validates the elements of an array to ensure that they are compatible with the declared type and raises
InvalidArgumentException if an incompatible type is encountered.
Slice dictionaries map to native PHP associative arrays. The PHP mapping does not currently support all Slice dictionary types, however, because native PHP associative arrays support only integer and string key types. A Slice dictionary whose key type is an enumeration or one of the primitive types
boolean,
byte,
short,
int, or
long is mapped as an associative array with an integer key.
1 A Slice dictionary with a string key type is mapped as associative array with a string key. All other key types cause a warning to be generated.
The Ice run time validates the elements of a dictionary to ensure that they are compatible with the declared type;
InvalidArgumentException exception is raised if an incompatible type is encountered.