0

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!

2 Answers 2

3

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", 
        // ...
    );

    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!! I can not vote you because I don't have enough reputation, but it works perfectly ;)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.