Voting

: min(six, eight)?
(Example: nine)

The Note You're Voting On

dmitrochenkooleg at gmail dot com
6 years ago
Get the latest constants declared.

abstract class AbstractEnum
{
/**
* Возвращает все константы класса || Return all constants
*
* @return array
*/
static function getConstants()
{
$rc = new \ReflectionClass(get_called_class());

return $rc->getConstants();
}

/**
* Возвращает массив констант определенные в вызываемом классе || Return last constants
*
* @return array
*/
static function lastConstants()
{
$parentConstants = static::getParentConstants();

$allConstants = static::getConstants();

return array_diff($allConstants, $parentConstants);
}

/**
* Возвращает все константы родительских классов || Return parent constants
*
* @return array
*/
static function getParentConstants()
{
$rc = new \ReflectionClass(get_parent_class(static::class));
$consts = $rc->getConstants();

return $consts;
}
}

======
class Roles extends AbstractEnum
{
const ROOT = 'root';
const ADMIN = 'admin';
const USER = 'user';
}

// Output:
All: root, admin, user
Last: root, admin, user

class NewRoles extends Roles
{
const CLIENT = 'client';
const MODERATOR = 'moderator';
const SUPERMODERATOR = 'super'.self::USER;
}

// Output:
All: client, moderator, superuser, root, admin, user
Last: client, moderator, superuser

class AdditionalRoles extends Roles
{
const VIEWER = 'viewer';
const CHECKER = 'checker';
const ROOT = 'rooter';
}

All: viewer, checker, rooter, client, moderator, superuser, admin, user
Last: viewer, checker, rooter

<< Back to user notes page

To Top