PHP Mapping for Structures

A Slice structure maps to a PHP class containing a public variable for each member of the structure. For example, here is our Employee structure once more:

Slice
struct Employee {
    long number;
    string firstName;
    string lastName;
};

The PHP mapping generates the following definition for this structure:

PHP
class Employee
{
    public function __construct($number=0, $firstName='', $lastName='');

    public function __toString();

    public $number;
    public $firstName;
    public $lastName;
}

The class 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:

Data Member TypeDefault Value
stringEmpty string
enumFirst enumerator in enumeration
structDefault-constructed value
NumericZero
boolFalse
sequenceNull
dictionaryNull
class/interfaceNull

You can also declare different default values for members of primitive and enumerated types.

The mapping also includes a definition for the __toString magic method, which returns a string representation of the structure.

See Also