PHP code example of belca / support-constants

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

    

belca / support-constants example snippets


use Belca\Support\AbstractEnum;

class MyConstants extends AbstractEnum
{
    const DEFAULT = self::USER;

    const USER = 'user';
    const ADMIN = 'administrator';
    const CLIENT = 'client';
}

// ...

$constants = MyConstants::list();

// Output $constants: [
//    'USER' => 'user',
//    'ADMIN' => 'superuser',
//    'CLIENT' => 'client',
// ]

$default = MyConstants::getDefault(); // only using AbstractEnum
// or
$default = MyConstants::DEFAULT;

// Output $default: 'user'

use Belca\Support\AbstractConstants;
// or
use Belca\Support\AbstractEnum;

namespace AnyoneVendor\MyPackage\Enums;

use Belca\Support\AbstractConstants;

class Roles extends AbstractConstants
{
    const USER = 'user';
    const SUPERUSER = 'superuser';
    const CLIENT = 'client';
    const MODERATOR = 'moderator';
    const SUPERMODERATOR = 'superuser';
}

$constants = Roles::list();
// or
$constants = Roles::getConstants();

// Output $constants: [
//    'USER' => 'user',
//    'SUPERUSER' => 'superuser',
//    'CLIENT' => 'client',
//    'MODERATOR' => 'moderator',
//    'SUPERMODERATOR' => 'superuser',
// ]

$user = Roles::getConst('USER'); // Output: 'user'
$superuser = Roles::getConst('SUPERUSER'); // Output: 'superuser'
$root = Roles::getConst('ROOT'); // Output: null, because it is not defined
$user = Roles::getConst('user'); // Output: null, because the constant was defined in uppercase

namespace App\Enums;

use AnyoneVendor\MyPackage\Enums\Roles as BaseRoles;

class Roles extends BaseRoles
{
    // Defines new constants
    const VIEWER = 'viewer';
    const CHECKER = 'checker';
    const TESTER = 'tester';

    // Replaces old values of constants
    const SUPERUSER = 'root';
    const SUPERMODERATOR = 'supermoderator';
}

Roles::isDefined('SUPERUSER'); // true
Roles::isDefined('ROOT'); // false

$constants = Roles::list();

// Output $constants: [
//    'USER' => 'user',
//    'SUPERUSER' => 'root',
//    'CLIENT' => 'client',
//    'MODERATOR' => 'moderator',
//    'SUPERMODERATOR' => 'superuser',
//    'VIEWER' => 'viewer',
//    'CHECKER' => 'checker',
// ]

$superuser = Roles::getConst('SUPERUSER'); // 'root'

$superuser = Roles::SUPERUSER; // 'root'
$root = Roles::ROOT; // Error: Undefined class constant 'ROOT'

namespace AnyoneVendor\MyPackage\Enums;

use Belca\Support\AbstractEnum;

class Roles extends AbstractEnum
{
    const DEFAULT = self::USER;

    const USER = 'user';
    const SUPERUSER = 'root';
    const CLIENT = 'client';
}

$default = Roles::getDefault(); // 'user'

$default = Roles::DEFAULT; // 'user'

namespace App\Enums;

use AnyoneVendor\MyPackage\Enums\Roles as BaseRoles;

class Roles extends BaseRoles
{
    const DEFAULT = self::UNREGISTRED;

    const UNREGISTERED = 'unregistered';
}
getConst($name)
getConst($name)