1

I have a PHP class with a few static properties like so:

static public $_TYPE_DESIGN_DRAFT = 'design_draft';
static public $_TYPE_STORYTELLING_DRAFT = 'storytelling_draft';
static public $_TYPE_OTHER = 'other';
static public $_TYPE_DATA_FILE = 'data_file';

static public $_ALLOWED_EXTENSIONS = array(       // on the next line it breaks
                self::$_TYPE_DESIGN_DRAFT => array("jpeg", "jpg", "png", "gif", "pdf", "txt", "rtf", "doc", "csv"),
                self::$_TYPE_STORYTELLING_DRAFT => array("jpeg", "jpg", "png", "gif", "pdf", "txt", "rtf", "doc", "csv"),
                self::$_TYPE_OTHER => array("jpeg", "jpg", "png", "gif", "pdf", "txt", "rtf", "doc", "csv"),
                self::$_TYPE_DATA_FILE => array("pdf", "txt", "rtf", "doc", "csv", "xls")
);

But it seems PHP won't let me define an array like that with the value of the keys as static properties of the same class.

A workaround would be to only define the $_ALLOWED_EXTENSIONS var in the constructor of the class, but then I can't make it static because when static, the constructor doesn't get called.

Is there an effective workaround for this?

6
  • the second line of your array definition contains a typo, you open parentheses but never close them: self::$_ALLOWED_EXTENSIONS => array( Commented Aug 29, 2012 at 12:38
  • Can you not create a static method to initialise the array? Commented Aug 29, 2012 at 12:41
  • 1
    Wouldn't a singleton or a lazy-instantiation static method be better? Commented Aug 29, 2012 at 12:45
  • @JeroenMoons the last line in the code has the close-parenthesis for $_ALLOWED_EXTENSIONS. Commented Aug 29, 2012 at 12:47
  • @Matt Now, yes. Has been fixed :) Commented Aug 29, 2012 at 12:48

1 Answer 1

3

You cannot use class properties like this.

Following on from my comment; can you not doing something like this?

class Test
{
    static public $_TYPE_DESIGN_DRAFT = 'design_draft';
    static public $_TYPE_STORYTELLING_DRAFT = 'storytelling_draft';
    static public $_TYPE_OTHER = 'other';
    static public $_TYPE_DATA_FILE = 'data_file';

    static public $_ALLOWED_EXTENSIONS = array();

    public static function init()
    {
        self::$_ALLOWED_EXTENSIONS = array(
            self::$_TYPE_DESIGN_DRAFT => array("jpeg", "jpg", "png", "gif", "pdf", "txt", "rtf", "doc", "csv"),
            self::$_TYPE_STORYTELLING_DRAFT => array("jpeg", "jpg", "png", "gif", "pdf", "txt", "rtf", "doc", "csv"),
            self::$_TYPE_OTHER => array("jpeg", "jpg", "png", "gif", "pdf", "txt", "rtf", "doc", "csv"),
            self::$_TYPE_DATA_FILE => array("pdf", "txt", "rtf", "doc", "csv", "xls")       
        );      
    }
}

Test::init();
print_r(Test::$_ALLOWED_EXTENSIONS);
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm yes, that would certainly be a solution. Not very elegant, but effective and afaik the only thing that worked :) Thanks!

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.