PHP code example of jesseschalken / enum

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

    

jesseschalken / enum example snippets


use JesseSchalken\Enum\IntEnum;

class Day extends IntEnum {
	const MONDAY    = 0;
    const TUESDAY   = 1;
    const WEDNESDAY = 2;
    const THURSDAY  = 3;
    const FRIDAY    = 4;
    const SATURDAY  = 5;
    const SUNDAY    = 6;

    public static function values() {
    	return array(
        	self::MONDAY,
        	self::TUESDAY,
        	self::WEDNESDAY,
        	self::THURSDAY,
        	self::FRIDAY,
        	self::SATURDAY,
        	self::SUNDAY,
        );
    }
}

function needs_day(Day $d) {
    if ($d->value() == Day::FRIDAY) {
    	print "It's Friday, Friday...";
    }
}

Day::check(Day::TUESDAY); // okay
Day::check(2); // okay
Day::check(7); // throws EnumException

Day::valid(Day::SUNDAY); // true
Day::valid('foo'); // false

$day = new Day(Day::MONDAY);

needs_day($day);

use JesseSchalken\Enum\StringEnum;

class Color extends StringEnum {
    const RED    = 'red';
    const ORANGE = 'orange';
    const YELLOW = 'yellow';
    const GREEN  = 'green';
    const BLUE   = 'blue';
    const PURPLE = 'purple';

	public static function values() {
    	return array(
            self::RED,
            self::ORANGE,
            self::YELLOW,
            self::GREEN,
            self::BLUE,
            self::PURPLE,
        );
    }
}

function needs_color(Color $c) {
	switch ($c->value()) {
    	case Color::RED:
        	print 'is red';
            break;
       	//...
    }
}

$color = new Color(Color::RED);

needs_color($color);