PHP Fatal error: Cannot access empty property

PHP Fatal error: Cannot access empty property

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Fatal error: Cannot access empty property

When debugging PHP, encountering a Fatal error: Cannot access empty property can be misleading if you aren't paying close attention to the preceding stack trace or notices. This specific error often appears when trying to assign a value to a variable that does not exist as a class property yet, but the code attempts to treat it as such.

In the scenario provided, the developer defines a class my_class with a property $my_value initialized as an empty array. However, there is a critical distinction between defining a property and assigning a value to it via dynamic variable syntax that can trip up beginners. The error occurs specifically in the set_value method:

function set_value ($value){
    // Error occurred from here as Undefined variable: my_value
    $this->$my_value = $value;
}

The root cause here is a classic variable scope confusion. Inside the set_value function, $my_value is referenced without the $ prefix inside the dynamic assignment operator ($this->$my_value). While PHP allows you to reference properties of the current object directly (e.g., $this->property_name), it does not automatically resolve unquoted property names as variables.

When you write $this->$my_value, PHP looks for a variable named $my_value within the local scope of that function. It does not look at the class definition or the parent object's properties to find a default value. Since $my_value has never been assigned a value inside set_value, it is effectively undefined in that specific context. Consequently, PHP throws a "Notice: Undefined variable" followed by a "Fatal error: Cannot access empty property" because the engine attempts to treat this non-existent variable as the name of the property to be accessed on $this.

The correct way to assign a value to an existing property dynamically is to use the curly brace syntax or simply reference the property directly if you know its name. If the intention is to overwrite the entire array stored in the property, the code should look like this:

function set_value ($value){
    // Correct approach: Direct assignment to the property
    $this->my_value = $value; 
}

Alternatively, if you intended to use a variable to hold the property name (e.g., $propName = 'my_value'), you would need to assign it first:

function set_value ($value) {
    $propertyName = 'my_value'; // Define the property name as a variable
    $this->$propertyName = $value;
}

There is also a secondary issue in the provided snippet regarding the constructor-like method named my_class. In PHP, constructors are typically named after the class without spaces, or simply follow standard naming conventions like __construct(). Using function my_class creates a regular instance method that returns nothing by default. When the code executes $a->my_class('d'), it is calling this method as if it were a constructor or a setter, but since it's just a void method, no state change occurs unless defined explicitly.

Furthermore, accessing properties directly via $a->my_value in the main script works because $my_value exists as a declared property of the my_class object. The error only strikes when the code tries to treat that property name as a dynamic variable string without resolving it first.

This pattern is common when transitioning from C-style languages where you might pass property names as strings or rely on different scoping rules. In PHP, strict typing and explicit property references prevent accidental overwrites of non-existent properties. If you are building larger applications where dynamic property access is necessary for polymorphism or configuration-driven logic, ensure that the variable containing the property name is explicitly initialized before using it in the $this->$var syntax.

For those looking to implement more robust data structures with dynamic keys or flexible storage patterns, frameworks like Laravel offer extensive support for these concepts within their Eloquent ORM and service container systems. Understanding how properties are resolved at runtime helps when working with advanced features found in modern PHP ecosystems.

Our network