is there a typedef
keyword in PHP such that I can do something like:
typedef struct {
} aStructure;
or
typedef enum {
aType1,
aType2,
} aType;
is there a typedef
keyword in PHP such that I can do something like:
typedef struct {
} aStructure;
or
typedef enum {
aType1,
aType2,
} aType;
You can do something similar with constants, but it's not the same as a dedicated enum.
Their is an Extension called SPL_Types, but this extension is nearly at no web hosting available AND its not maintained anymore. So the best would be using classes for structs. and constants for enums. maybe with the help of the plain SPL extension, which is nearly in every php 5.X installation available, you could build some "evil dirty enum hack"
php has two (count them -- 2) datatypes.
A single scaler value stored as a piece of text which will be converted to a number or boolean in an arithmetic or logical context where possable.
The second structure is a hash (not an Array!) which is keyed by a scaler text. If the key is a numeric value then the hash beheaves very much like an array, if its a text value then it beheaves more like a classic perl hash.
You can 'fake' an enum using an inverted hash/array structure:
%pretend_enum = array ( 'cent' => 1, 'nickel' => 2, 'dime' => 3 );
if ($pretend_enum[$value]) {
$encoded = $pretend_enum[$value];
} else {
echo "$value is not a valid coin";
}
"Structures" are usually faked by having a hash with named members:
$ceedee = array('title' => "Making Movies",'artist' => "Dire Straights",'tracks' => 12);
echo "my favourate cd is " . $ceedee['title'];
Here is a github library for handling type-safe enumerations in php:
This library handle classes generation, classes caching and it implements the Type Safe Enumeration design pattern, with several helper methods for dealing with enums, like retrieving an ordinal for enums sorting, or retrieving a binary value, for enums combinations.
The generated code use a plain old php template file, which is also configurable, so you can provide your own template.
It is full test covered with phpunit.
php-enums on github (feel free to fork)
<?php
//require the library
require_once __DIR__ . '/src/Enum.func.php';
//if you don't have a cache directory, create one
@mkdir(__DIR__ . '/cache');
EnumGenerator::setDefaultCachedClassesDir(__DIR__ . '/cache');
//Class definition is evaluated on the fly:
Enum('FruitsEnum', array('apple' , 'orange' , 'rasberry' , 'bannana'));
//Class definition is cached in the cache directory for later usage:
Enum('CachedFruitsEnum', array('apple' , 'orange' , 'rasberry' , 'bannana'), '\my\company\name\space', true);
echo 'FruitsEnum::APPLE() == FruitsEnum::APPLE(): ';
var_dump(FruitsEnum::APPLE() == FruitsEnum::APPLE()) . "\n";
echo 'FruitsEnum::APPLE() == FruitsEnum::ORANGE(): ';
var_dump(FruitsEnum::APPLE() == FruitsEnum::ORANGE()) . "\n";
echo 'FruitsEnum::APPLE() instanceof Enum: ';
var_dump(FruitsEnum::APPLE() instanceof Enum) . "\n";
echo 'FruitsEnum::APPLE() instanceof FruitsEnum: ';
var_dump(FruitsEnum::APPLE() instanceof FruitsEnum) . "\n";
echo "->getName()\n";
foreach (FruitsEnum::iterator() as $enum)
{
echo " " . $enum->getName() . "\n";
}
echo "->getValue()\n";
foreach (FruitsEnum::iterator() as $enum)
{
echo " " . $enum->getValue() . "\n";
}
echo "->getOrdinal()\n";
foreach (CachedFruitsEnum::iterator() as $enum)
{
echo " " . $enum->getOrdinal() . "\n";
}
echo "->getBinary()\n";
foreach (CachedFruitsEnum::iterator() as $enum)
{
echo " " . $enum->getBinary() . "\n";
}
FruitsEnum::APPLE() == FruitsEnum::APPLE(): bool(true)
FruitsEnum::APPLE() == FruitsEnum::ORANGE(): bool(false)
FruitsEnum::APPLE() instanceof Enum: bool(true)
FruitsEnum::APPLE() instanceof FruitsEnum: bool(true)
->getName()
APPLE
ORANGE
RASBERRY
BANNANA
->getValue()
apple
orange
rasberry
bannana
->getValue() when values have been specified
pig
dog
cat
bird
->getOrdinal()
1
2
3
4
->getBinary()
1
2
4
8