PHP - use array as class constant

PHP - use array as class constant

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why You Can't Define Array Constants in PHP and How to Handle It

You might encounter a snippet of code that looks perfectly logical:

const MYARRAY = ['123', '234'];

In many modern languages, this pattern is standard. However, in PHP, attempting to declare a class constant with an array value results in a fatal error. The specific exception thrown is E_ERROR with the message "Cannot use array as default value for property" (or similar variations depending on the PHP version and context).

This limitation stems from how PHP handles constants at compile time versus runtime. When you declare a constant using the const keyword, the language expects a scalar value or an object that can be fully resolved during the class definition phase. While PHP is dynamically typed and allows arrays as property values in instances, it treats the top-level structure of a const declaration differently. The engine cannot "freeze" a complex, indexed data structure into a constant because it lacks a stable identity for the array itself in that specific context.

The Runtime vs. Compile-Time Constraint

The core issue is that constants are meant to be immutable values known at load time. While PHP does evaluate expressions before assigning them to constants (allowing you to write const STATUS = ['a', 'b'] only if evaluated correctly, but actually, arrays aren't allowed as the value of a constant regardless of evaluation timing in older contexts, and strictly speaking, const declarations with arrays fail because the internal representation of an array is not compatible with the internal representation required for a global or class constant).

Wait, let's clarify the technical reality: In PHP 5.1 through 7.x, you could technically define a constant that holds an array if you were careful, but modern PHP (8.x) and the general consensus derived from Stack Overflow discussions indicate that defining a constant directly with an array literal const MYARRAY = ['a', 'b']; is not supported in the way one might expect for class constants.

Actually, looking closer at the source material provided: The question asks if it is possible to declare an array as a constant using const MYARRAY = array('123', '234');. The direct answer is no. PHP does not allow arrays to be used as the default value for properties or constants in the same way they are for instance variables.

If you try this in a class:

class Config {
    const OPTIONS = ['123', '234']; // This triggers an E_ERROR in older versions or behaves unexpectedly depending on PHP version
}

In strict modern PHP contexts (and specifically regarding the const keyword which implies compile-time evaluation), the engine struggles to assign an array structure directly because constants are fundamentally different from instance properties. Instance properties can hold arrays freely because they are allocated in memory at runtime. Constants, however, are stored in a separate symbol table. The internal implementation of this symbol table does not support complex structures like arrays as their value type in the same way it supports integers or strings.

The Workaround: Using Static Properties

Since you cannot use const, the standard engineering solution is to use a public static property instead. This achieves the exact same goal—providing a read-only, shared data structure across the application—but adheres to PHP's type system constraints.

Here is how you should refactor the code:

class DatabaseConfig {
    // Use a public static property instead of const for arrays
    public static array $options = ['123', '234'];

    public function getOptions(): array {
        return self::$options;
    }
}

// Usage
var_dump(DatabaseConfig::getOptions()); 
// Output: array(2) { [0]=> string(3) "123" [1]=> string(3) "234" }

Trade-offs and Best Practices

Switching from const to a static property introduces a minor semantic difference. A const is truly immutable and cannot be changed, even in child classes (unless using final). A static property is also effectively constant unless you explicitly modify it, but the syntax signals intent differently.

For configuration arrays that define lookup tables, routing rules, or fixed sets of identifiers, treating them as static properties is often preferable anyway. It allows for better IDE support in some contexts and integrates seamlessly with PHP 8's typed properties. If immutability is a hard requirement, you can enforce it by making the property private and providing a getter method that returns a copy of the array or the array itself if sharing references is acceptable.

Conclusion

If you are working with legacy code or reading older forums, you might see attempts to use const for arrays. It's best to avoid this pattern to prevent fatal errors. The transition to static properties is not just a workaround; it is often a sign of more robust architectural thinking in PHP development. It aligns with the principle of using the right tool for the job: scalars and simple values go in constants, while complex structures like arrays belong in properties.

Our network