lundi 15 février 2016

PHP Enum Hierarchy

I want to create hierarchical Enumarations for my PHP Applications and thought of things like

abstract class User_Roles extends Enum {

    const Administrator = "";
    const Account_Manager = "Administrator";
    const BlogAuthor = "Account_Manager";
    const CommentManager = "Account_Manager";

}

I'm using this Enum-Class: SO

So each child has his parent Node as it's value.

This is how I would do this:

    $constants = $reflector->getConstants();
    foreach ($constants as $key => $value) {
        if ($value == "") {
           $returnHierarchy[$key] = array();
           continue;
        }
        $returnHierarchy[][$value] = $key;
    }

But I have some issues with the multidimensional array which I want to create.

So it should look like this:

[Administrator]
{
    [Account_Manager]
    {
        [BlogAuthor]
        [CommentManager]
    }
}

But I end up in stuff like this:

array(4) (
  [Administrator] => array(0)
  [0] => array(1) (
    [Administrator] => (string) Account_Manager
  )
  [1] => array(1) (
    [Account_Manager] => (string) BlogAuthor
  )
  [2] => array(1) (
    [Account_Manager] => (string) CommentManager
  )
)

Is there anything that I misunderstand or overlook?





Aucun commentaire:

Enregistrer un commentaire