PHP and Enumerations

后端 未结 30 2030
有刺的猬
有刺的猬 2020-11-22 13:39

I know that PHP doesn\'t have native Enumerations. But I have become accustomed to them from the Java world. I would love to use enums as a way to give predefined values whi

30条回答
  •  旧巷少年郎
    2020-11-22 13:59

    I realize this is a very-very-very old thread but I had a thought about this and wanted to know what people thought.

    Notes: I was playing around with this and realized that if I just modified the __call() function that you can get even closer to actual enums. The __call() function handles all unknown function calls. So let's say you want to make three enums RED_LIGHT, YELLOW_LIGHT, and GREEN_LIGHT. You can do so now by just doing the following:

    $c->RED_LIGHT();
    $c->YELLOW_LIGHT();
    $c->GREEN_LIGHT();
    

    Once defined all you have to do is to call them again to get the values:

    echo $c->RED_LIGHT();
    echo $c->YELLOW_LIGHT();
    echo $c->GREEN_LIGHT();
    

    and you should get 0, 1, and 2. Have fun! This is also now up on GitHub.

    Update: I've made it so both the __get() and __set() functions are now used. These allow you to not have to call a function unless you want to. Instead, now you can just say:

    $c->RED_LIGHT;
    $c->YELLOW_LIGHT;
    $c->GREEN_LIGHT;
    

    For both the creation and getting of the values. Because the variables haven't been defined initially, the __get() function is called (because there isn't a value specified) which sees that the entry in the array hasn't been made. So it makes the entry, assigns it the last value given plus one(+1), increments the last value variable, and returns TRUE. If you set the value:

    $c->RED_LIGHT = 85;
    

    Then the __set() function is called and the last value is then set to the new value plus one (+1). So now we have a fairly good way to do enums and they can be created on the fly.

    enums = array();
        $this->clear_flag = false;
        $this->last_value = 0;
    
        if( func_num_args() > 0 ){
            return $this->put( func_get_args() );
            }
    
        return true;
    }
    ################################################################################
    #   put(). Insert one or more enums.
    ################################################################################
    function put()
    {
        $args = func_get_args();
    #
    #   Did they send us an array of enums?
    #   Ex: $c->put( array( "a"=>0, "b"=>1,...) );
    #   OR  $c->put( array( "a", "b", "c",... ) );
    #
        if( is_array($args[0]) ){
    #
    #   Add them all in
    #
            foreach( $args[0] as $k=>$v ){
    #
    #   Don't let them change it once it is set.
    #   Remove the IF statement if you want to be able to modify the enums.
    #
                if( !isset($this->enums[$k]) ){
    #
    #   If they sent an array of enums like this: "a","b","c",... then we have to
    #   change that to be "A"=>#. Where "#" is the current count of the enums.
    #
                    if( is_numeric($k) ){
                        $this->enums[$v] = $this->last_value++;
                        }
    #
    #   Else - they sent "a"=>"A", "b"=>"B", "c"=>"C"...
    #
                        else {
                            $this->last_value = $v + 1;
                            $this->enums[$k] = $v;
                            }
                    }
                }
            }
    #
    #   Nope!  Did they just sent us one enum?
    #
            else {
    #
    #   Is this just a default declaration?
    #   Ex: $c->put( "a" );
    #
                if( count($args) < 2 ){
    #
    #   Again - remove the IF statement if you want to be able to change the enums.
    #
                    if( !isset($this->enums[$args[0]]) ){
                        $this->enums[$args[0]] = $this->last_value++;
                        }
    #
    #   No - they sent us a regular enum
    #   Ex: $c->put( "a", "This is the first enum" );
    #
                        else {
    #
    #   Again - remove the IF statement if you want to be able to change the enums.
    #
                            if( !isset($this->enums[$args[0]]) ){
                                $this->last_value = $args[1] + 1;
                                $this->enums[$args[0]] = $args[1];
                                }
                            }
                    }
                }
    
        return true;
    }
    ################################################################################
    #   get(). Get one or more enums.
    ################################################################################
    function get()
    {
        $num = func_num_args();
        $args = func_get_args();
    #
    #   Is this an array of enums request? (ie: $c->get(array("a","b","c"...)) )
    #
        if( is_array($args[0]) ){
            $ary = array();
            foreach( $args[0] as $k=>$v ){
                $ary[$v] = $this->enums[$v];
                }
    
            return $ary;
            }
    #
    #   Is it just ONE enum they want? (ie: $c->get("a") )
    #
            else if( ($num > 0) && ($num < 2) ){
                return $this->enums[$args[0]];
                }
    #
    #   Is it a list of enums they want? (ie: $c->get( "a", "b", "c"...) )
    #
            else if( $num > 1 ){
                $ary = array();
                foreach( $args as $k=>$v ){
                    $ary[$v] = $this->enums[$v];
                    }
    
                return $ary;
                }
    #
    #   They either sent something funky or nothing at all.
    #
        return false;
    }
    ################################################################################
    #   clear(). Clear out the enum array.
    #       Optional.  Set the flag in the __construct function.
    #       After all, ENUMS are supposed to be constant.
    ################################################################################
    function clear()
    {
        if( $clear_flag ){
            unset( $this->enums );
            $this->enums = array();
            }
    
        return true;
    }
    ################################################################################
    #   __call().  In case someone tries to blow up the class.
    ################################################################################
    function __call( $name, $arguments )
    {
        if( isset($this->enums[$name]) ){ return $this->enums[$name]; }
            else if( !isset($this->enums[$name]) && (count($arguments) > 0) ){
                $this->last_value = $arguments[0] + 1;
                $this->enums[$name] = $arguments[0];
                return true;
                }
            else if( !isset($this->enums[$name]) && (count($arguments) < 1) ){
                $this->enums[$name] = $this->last_value++;
                return true;
                }
    
        return false;
    }
    ################################################################################
    #   __get(). Gets the value.
    ################################################################################
    function __get($name)
    {
        if( isset($this->enums[$name]) ){ return $this->enums[$name]; }
            else if( !isset($this->enums[$name]) ){
                $this->enums[$name] = $this->last_value++;
                return true;
                }
    
        return false;
    }
    ################################################################################
    #   __set().  Sets the value.
    ################################################################################
    function __set( $name, $value=null )
    {
        if( isset($this->enums[$name]) ){ return false; }
            else if( !isset($this->enums[$name]) && !is_null($value) ){
                $this->last_value = $value + 1;
                $this->enums[$name] = $value;
                return true;
                }
            else if( !isset($this->enums[$name]) && is_null($value) ){
                $this->enums[$name] = $this->last_value++;
                return true;
                }
    
        return false;
    }
    ################################################################################
    #   __destruct().  Deconstruct the class.  Remove the list of enums.
    ################################################################################
    function __destruct()
    {
        unset( $this->enums );
        $this->enums = null;
    
        return true;
    }
    
    }
    #
    #   Test code
    #
    #   $c = new enums();
    #   $c->RED_LIGHT(85);
    #   $c->YELLOW_LIGHT = 23;
    #   $c->GREEN_LIGHT;
    #
    #   echo $c->RED_LIGHT . "\n";
    #   echo $c->YELLOW_LIGHT . "\n";
    #   echo $c->GREEN_LIGHT . "\n";
    
    ?>
    

提交回复
热议问题