I have lists where strings correspond with values. For example, in a company list I could have Microsoft correspond with the letters MS.
Obviously, this could be transposed as a table in MySQL to make the list extensible but, from curiosity, how would you express this in constant form in PHP ?
P.S: There is the class-with-constants approach (see accepted answer here: PHP and Enumerations) to act as an enumeration but would that really be of any use seeing that enumerations map to integer values ?
How about using define
define("MS","Microsoft");
echo MS;
This would echo Microsoft.
http://php.net/manual/en/function.define.php
Also the link you gave to a possible solution could just as easily be used with strings instead. The only reason it acts as an enum from other languages is because you define the values from 0 to n, instead of doing that just use string instead.
class Companies {
const MS = 'Microsoft';
const IBM = 'International Business Machines';
}
echo Companies::MS;
I think this would work.
i would probably start by looking at multi dimentional arrays if a single company can have many corresponding letters, however the draw back being that they can get difficult to manage, your other option is to define constants
If you have key-value pairs you can put those into an object, so its more practical to use.
check on this link: the arrayToObject() function