PHP code example of petrknap / enum

1. Go to this page and download the library: Download petrknap/enum 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/ */

    

petrknap / enum example snippets


use PetrKnap\Enum\Readme\MyBoolean;

$isTrue = function (int $myBoolean)
{
    switch($myBoolean) {
        case MyBoolean::MY_TRUE:
            return true;
        case MyBoolean::MY_FALSE:
            return false;
    }
};

var_dump($isTrue(MyBoolean::MY_TRUE));  // true - correct
var_dump($isTrue(MyBoolean::MY_FALSE)); // false - correct
var_dump($isTrue(0));                   // none
var_dump($isTrue(1));                   // true - expected
var_dump($isTrue(2));                   // false
var_dump($isTrue((int) true));          // true - expected
var_dump($isTrue((int) false));         // none

use PetrKnap\Enum\Readme\MyBoolean;

$isTrue = function (MyBoolean $myBoolean): bool
{
    switch($myBoolean) {
        case MyBoolean::MyTrue:
            return true;
        case MyBoolean::MyFalse:
            return false;
    }
};

var_dump($isTrue(MyBoolean::MyTrue));  // true - correct
var_dump($isTrue(MyBoolean::MyFalse)); // false - correct