1. Go to this page and download the library: Download vijinho/enums library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// get an enum value by key
echo Fruits::apple(); // Apple
echo Fruits::APPLE(); // Apple
// add a key => value to the enum
Fruits::add([
'STRAWBERRY' => 'Strawberry',
'Avocado' => 'Avocado'
]);
// alternative way to fetch a value by key
echo Fruits::value('strawberry'); // Strawberry
// return the key for a value
echo Fruits::key('cucumber'); // tomato
// return all fruits
print_r(Fruits::values());
/*
(
[apple] => Apple
[pear] => Pear
[banana] => Banana
[orange] => Orange
[grapefruit] => Grapefruit
[tomato] => Cucumber
[STRAWBERRY] => Strawberry
[Avocado] => Avocado
)
*/
$f = new Fruits;
$f(['mango']); // add a new fruit - magic!
$f(['pineapple' => 'Pineapple']); // add another new fruit
$f->add(['potato' => 'Not a fruit']);
var_dump($f); // special var_dump magic!
object(Fruits)#5 (5) {
["overwrite"]=>
bool(false)
["delete"]=>
bool(false)
["capitalize"]=>
bool(false)
["caseSensitive"]=>
bool(false)
["values"]=>
array(11) {
["apple"]=>
string(5) "Apple"
["pear"]=>
string(4) "Pear"
["banana"]=>
string(6) "Banana"
["orange"]=>
string(6) "Orange"
["grapefruit"]=>
string(10) "Grapefruit"
["tomato"]=>
string(8) "Cucumber"
["STRAWBERRY"]=>
string(10) "Strawberry"
["Avocado"]=>
string(7) "Avocado"
["mango"]=>
string(5) "mango"
["pineapple"]=>
string(9) "Pineapple"
["potato"]=>
string(11) "Not a fruit"
}
}
use \vijinho\Enums\Enum;
// create a new enum $e
echo "Example 1\n";
$e = new Enum(['apple', 'pear', 'peach']);
// retrieve apple using array access
echo $e['apple']; // apple
// retrieve apple using array access
echo "Example 2\n";
echo isset($e['pear']); // 1
// remove a value
unset($e['pear']);
echo $e;
/*
{
"apple": "apple",
"peach": "peach"
}
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.