PHP Mapping for Constants

A Slice constant maps to a PHP constant. Consider the following definitions:

Slice
module M {
    const bool      AppendByDefault = true;
    const byte      LowerNibble = 0x0f;
    const string    Advice = "Don't Panic!";
    const short     TheAnswer = 42;
    const double    PI = 3.1416;

    enum Fruit { Apple, Pear, Orange };
    const Fruit     FavoriteFruit = Pear;
};

The mapping for these constants is shown below:

PHP
define('M_AppendByDefault', true);
define('M_LowerNibble', 15);
define('M_Advice', "Don't Panic!");
define('M_TheAnswer', 42);
define('M_PI', 3.1416);
define('M_FavoriteFruit', M_Fruit::Pear);

Slice string literals that contain non-ASCII characters or universal character names are mapped to PHP string literals with these characters replaced by their UTF-8 encoding as octal escapes. For example:

Slice
module M {
   const string Egg = "œuf";
   const string Heart = "c\u0153ur";
   const string Banana = "\U0001F34C";
};

is mapped to:

PHP
define('M_Egg', "\305\223uf");
define('M_Heart', "c\305\223ur");
define('M_Banana', "\360\237\215\214");

An application refers to a constant using its flattened name:

PHP
$ans = M_TheAnswer;

Using the namespace mapping, Slice constants are mapped to PHP constants in the enclosing namespace:

PHP
$ans = \M\TheAnswer;

See Also