Angel
Angel

Reputation: 1980

declare make static array php with variables

I'm facing some troubles to make a static array, giving some static attributes of the class as keys of the static array, something like this:

class A {

    private $ambito; //will be filled with an element of the static $ambitos

    public static $municipal = 1;
    public static $provincial = 2;
    public static $regional = 3;

    /*array para declarar los posibles ambitos de visualizacion de una empresa*/
    private static $ambitos = array( 
                                   self::$municipal => "Municipal", 
                                   self::$provincial => "Provincial", 
                                   self::$regional => "Regional"
                              );



    public static function getAmbitos()
    {
        return self::$ambitos;
    }
}

The problem is that I can't use self:$municipal inside the static array, because it fire an error, I only can use literal integer (is how I saved in the database)

I'm using symphony 2.0.

Thanks!

Upvotes: 0

Views: 1139

Answers (2)

pozs
pozs

Reputation: 36274

You can use constants there too.

class A {

    const DEFAULT_MUNICIPAL = 1;

    // ...

    public static $municipal = self::DEFAULT_MUNICIPAL;

    // ...

    private static $ambitos = array( 
        self::DEFAULT_MUNICIPAL => "Municipal", 
        // ...
    );

    // ...
}

Upvotes: 3

Oussama Jilal
Oussama Jilal

Reputation: 7739

You can not reference a static variable in another static variable (since they will be parsed at the same time)

Upvotes: 0

Related Questions