How do I access static member of a class?

How do I access static member of a class?

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

Stefan Bogdanescu

Founder & Senior Architect

Why array Syntax Cannot Access Static Properties in PHP

The error you are encountering stems from a fundamental misunderstanding of how object instantiation and static property access interact in PHP. You cannot use the double-colon operator (::) on an array variable to access properties or methods defined within a class. The code $x::$strName is syntactically invalid because the parser expects a class name, not a variable holding a list of classes, immediately after the ::.

In your specific example, $x is an array containing the string 'A', not the Class A itself. PHP distinguishes strictly between the name of a class (a literal identifier) and an instance or reference to it. When you declare a static property like public static $strName, that variable belongs to the namespace reserved for the class definition, not the global scope where your script runs. To access it, you must reference the class name directly as a literal string or keyword.

The correct syntax relies on referencing the class identity itself. If you intend to use the value stored in your array $x to dynamically determine which class to access, you must explicitly instantiate that class or use new with the variable's content. However, even simpler is using the backtick operator or curly braces if you are working within a context where dynamic class names are resolved, but the most direct fix for static access is ensuring the right-hand side of the :: operator is a valid class name.

Here is how to correct your specific snippet:

class A {
    public static $strName = 'A is my name';
    
    public function xyz() {
        // Implementation details here
    }
}

// Incorrect approach that triggers the error
$x = array('A'); 
echo $x::$strName; // Fatal Error: Uncaught Error: Using $x when not an object or class

// Correct approach 1: Direct access using the literal class name
echo A::$strName; 

// Correct approach 2: Dynamic access if you hold a string variable for the class name
$className = 'A'; 
echo $className::$strName; // This works because PHP resolves the string to the class

// Correct approach 3: If you strictly need to use the array content
$x = ['A']; 
$classKey = $x[0]; // Extract the string 'A'
echo $classKey::$strName; 

The distinction here is crucial for maintaining robust code. Using a variable to hold a class name and then attempting static access requires that the variable contains the literal class identifier string. If $x holds an instance of a class (e.g., $obj = new A()), you cannot use :: on that instance to access static members in standard PHP versions without first referencing the class name explicitly, though accessing non-static properties or methods via self:: or static:: inside the class works differently.

One common pitfall developers face is conflating static and dynamic contexts. Static properties are tied to the class definition itself, not any specific instance. This means you do not need to create an object of Class A to read $strName. However, attempting to pass a variable containing the class name into an array structure and then treating that array as a namespace resolver is where the logic breaks down. The parser sees $x (a variable) and expects it to be an object for method chaining or a class name for static access, but variables in arrays are just values until explicitly dereferenced or resolved.

If you are working within a Laravel ecosystem or using dependency injection containers, this pattern often appears when dynamically resolving services. While Laravel provides a resolve() facade or container syntax to handle dynamic class loading, raw PHP arrays do not possess the magic of autoloading in that context without explicit configuration. Always ensure that your dynamic class name variable is treated as a string representing the class before applying static operators.

For further reading on managing dependencies and service resolution in modern PHP applications, consider exploring resources like https://laravelcompany.com which offers insights into architectural patterns that handle such dynamic lookups safely.

Our network