PHP code example of intraworlds / enum

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

    

intraworlds / enum example snippets


final class Hash extends Enum
{
  const MD5 = 'md5';
  const SHA1 = 'sha1';
}

$md5 = Hash::MD5();

function crack(Hash $hash) {
  echo 'cracking ... ' . $hash; // notice that enum is implementing __toString() method
}

crack(Hash::SHA1()); // cracking ... sha1
crack(Hash::SHA1);   // throws TypeError

var_dump($md5 === MD5()); // true
var_dump($md5 === SHA1()); // false

var_dump($md5 == MD5()); // true
var_dump($md5 == SHA1()); // true - DON'T use == comparison!

var_dump($md5->getValue() === Hash::MD5); // true

switch ($hash->getValue()) {
  case Hash::MD5: ...
  case Hash::SHA1: ...
}

$contentType = ContentTypeEnum::search(apache_request_headers()['Content-Type']);