PHP - define static array of objects

PHP - define static array of objects

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

Stefan Bogdanescu

Founder & Senior Architect

PHP - define static array of objects

You can definitely initialize a static array of simple values, like strings or integers, directly inside a class definition. The syntax public static $blah = array("test1", "test2"); works perfectly because the constructor for these primitives is executed during the static analysis phase or at compile time in languages with JITs, but in PHP specifically, it just evaluates to the literal value before any object instance exists.

The problem arises when you attempt to instantiate objects inside that same declaration block. Writing public static $blah2 = array(new MyOtherClass(), new MyOtherClass()); often results in a fatal error or unexpected behavior depending on the PHP version and autoloading setup. The core issue is not just that you are creating objects, but when and how they are being created relative to the class definition itself.

When PHP parses a file, it reads from top to bottom. If MyOtherClass hasn't been fully loaded or defined yet when MyClass tries to reference it, you get an "Undefined class constant" or "Cannot access private/protected property" error if the objects are holding state. Even worse, creating objects at parse time can trigger side effects (like database connections or file writes inside constructors) before your application has a chance to handle errors.

The standard workaround is to separate the definition of the collection from its initialization. Instead of trying to run new during the class declaration, you define an empty static array and populate it in a dedicated method. This ensures that the objects are instantiated only when they are needed, or at least after the autoloader has guaranteed that the class definitions are ready.

class MyClass {
    public static $blah2 = []; // Initialize as empty array

    public static function init() {
        self::$blah2 = [
            new MyOtherClass(),
            new MyOtherClass(),
            new MyOtherClass()
        ];
    }
}

// Usage
MyClass::init();
print_r(MyClass::$blah2);

This pattern is robust because it separates concerns: the class structure defines what the data can be, while the initialization logic handles the creation. However, this introduces a slight friction in usage; developers must remember to call the init method before accessing the static property if they expect data to exist immediately.

There are scenarios where you absolutely cannot use this approach, though. If MyOtherClass relies on static state that is initialized globally (perhaps a singleton registry or a shared configuration loaded at startup), calling its constructor repeatedly inside an array might be inefficient or redundant. In such cases, you might want to initialize the collection once at the very top of your application entry point, rather than inside a class method.

// In bootstrap.php or entry point
MyClass::init();

class MyClass {
    public static $blah2 = []; 
}

Alternatively, if you are working within the Laravel ecosystem, you might consider moving this data to a Service Container binding or a repository pattern instead of maintaining a static array. Static arrays in classes can lead to memory leaks if objects hold open resources (file handles, database connections) that aren't properly cleaned up, and they bypass dependency injection entirely.

If you must keep the data static and tightly coupled to the class, ensure that the constructors for your objects are lightweight. Avoid long-running operations or heavy resource acquisition in the __construct method of MyOtherClass. If those objects need external state, pass dependencies via constructor injection rather than accessing global variables inside the array initialization block.

Ultimately, the error you encountered is a safeguard against premature instantiation and undefined class references. By shifting the object creation to an explicit method call, you gain control over the lifecycle of those instances and avoid parsing-time crashes. This approach aligns with modern PHP best practices where static properties are treated as containers for data that is populated dynamically, rather than pre-filled constants.

Our network