PHP code example of morebec / orkestra-enum

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

    

morebec / orkestra-enum example snippets


class CardinalPoint extends Enum
{
    const NORTH = 'NORTH';    
    const EAST = 'EAST';    
    const WEST = 'WEST';  
    const SOUTH = 'SOUTH';
}

// Instantiate a new CardinalPoint instance
$direction = new CardinalPoint(CardinalPoint::NORTH);

// Since Enums have builtin validation,
// the following line would throw an InvalidArgumentException:
$direction = new CardinalPoint('North');

// However the following would work:
$direction = new CardinalPoint('NORTH');

// Using in functions or class methods 
public function changeDirection(CardinalPoint $direction)
{
    // Testing equlity with string
    if(!$direction->isEqualTo(new CardinalPoint(CardinalPoint::EAST))) {
        echo 'Not going East!';
    }

    // Since the constants are strings, it is also possible to compare
    // using loose comparison
    if($direction == CardinalPoint::NORTH) {
        echo 'Definitely going North!';
    }    
}

> /**
 > * @method static self NORTH() 
 > * @method static self EAST() 
 > * @method static self WEST() 
 > * @method static self SOUTH() 
 > */
> class CardinalPoint extends Enum
> {
>    const NORTH = 'NORTH';    
>    const EAST = 'EAST';    
>    const WEST = 'WEST';  
>    const SOUTH = 'SOUTH';
> }
> 

> $direction = CardinalPoint::NORTH();
> 

CardinalPoint::getValues(); 
// Returns an array as: 
// [ 'NORTH', 'EAST', 'WEST', 'SOUTH' ]