0

How do I access values of a static array in PHP?

$anyVar = self::MY_ARRAY['identifier']

throws 'unexpected [ after MY_ARRAY' at me.

1 Answer 1

1

You're missing your dollar sign

$anyVar = self::$MY_ARRAY['identifier'];

FYI, naming variables in uppercase should be reserved to constants by convention.

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

3 Comments

Ok, however const MY_ARRAY = array("key" => "value", "key2" => "value2"); doesn't seem to be allowed
So, whats the naming convention for static varialbes? And why isn't there a final that expresses that a varialbe has a set value that cannot chane anymore? Weired language...
No need to make the array a constant. Just use lowercase for the variable name. Static variables don't have a naming convention (unless you're part of a team that declares their own).

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.