PHP code example of joskolenberg / enum

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

    

joskolenberg / enum example snippets


use JosKolenberg\Enum\Enum;

class UserType extends Enum {}

use JosKolenberg\Enum\Enum;

class UserType extends Enum
{

    protected $id;

    protected $name;

    public function __construct($id, $name)
    {
        $this->id = $id;
        $this->name = $name;
    }

    /**
     * Seed the class with Enum instances
     *
     * @return array
     */
    protected static function seed()
    {
        return [
            new static('dev', 'Developer'),
            new static('admin', 'Administrator'),
            new static('user', 'User'),
        ];
    }

    /**
     * Return the name of the attribute which stores the identifier
     *
     * @return string
     */
    protected function identifierAttribute()
    {
        return 'id';
    }
}

class UserType extends Enum
{

    protected $id;
    protected $name;

    protected static function seed()
    {
        return [
            new UserTypeDev(),
            new UserTypeAdmin(),
        ];
    }

    protected function identifierAttribute()
    {
        return 'id';
    }
}

class UserTypeDev extends UserType{
    
    public function __construct()
    {
        $this->id = 'dev';
        $this->name = 'Developer';
    }

    public function whatCanYouDo()
    {
        return 'I can code';
    }
}

class UserTypeAdmin extends UserType{
    
    public function __construct()
    {
        $this->id = 'admin';
        $this->name = 'Administrator';
    }

    public function whatCanYouDo()
    {
        return 'I can do some important stuff';
    }
}

    protected static function newCollection(array $enums = [])
    {
        return new MyCustomEnumCollection($enums);
    }

class UserStatus extends \JosKolenberg\Enum\EnumWithId
{
    protected static function seed()
    {
        return [
            new static('new'),
            new static('active'),
            new static('deleted'),
        ];
    }
}

class ExtendedUserType extends \JosKolenberg\Enum\EnumWithIdAndName
{
    protected static function seed()
    {
        return [
            new static('dev', 'Developer'),
            new static('admin', 'Administrator'),
            new static('user', 'User'),
        ];
    }
}