How to access a class object property? $this->$property1 gives a "Cannot access empty property" error

How to access a class object property? $this->$property1 gives a "Cannot access empty property" error

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

Stefan Bogdanescu

Founder & Senior Architect

The Dynamic Property Syntax Trap in PHP

You are likely familiar with accessing standard object properties using static-like syntax: $this->propertyName. This works seamlessly for any property defined in your class or inherited from a parent. However, developers frequently encounter a specific error when attempting to use the curly-brace variable syntax: $this->$propertyName. The immediate feedback is often a fatal error stating "PHP: Cannot access empty property".

This error message can be confusing because PHP supports dynamic properties perfectly well in many contexts. The root cause lies in how PHP interprets the syntax versus what actually exists in memory at runtime.

Static vs. Dynamic Access

To understand why the error occurs, you must distinguish between two distinct access patterns:

  1. Static Property Access: $this->property1
    This syntax tells the parser to look for a property literally named property1. If that property exists in the object's scope, it is returned. If it does not exist, PHP throws an "Undefined property" notice (unless error reporting is suppressed), but it never triggers the "empty property" fatal error.

  2. Dynamic Property Access: $this->$property1
    Here, property1 is a string variable containing the name of the property you wish to access. PHP evaluates property1 at runtime and attempts to retrieve that specific key from the object.

The "Cannot access empty property" error specifically triggers when using the dynamic syntax ($this->$var) on an object that does not contain the property referenced by $var.

Why the Error Happens

Consider a standard class definition:

class User {
    public $name = 'John';
}

$user = new User();

// This works fine
echo $user->name; // Output: John

Now, imagine you have a variable holding the property name you want to access:

$targetProp = 'name'; 

// Correct usage
echo $user->$targetProp; // Output: John

The confusion arises when $user is initialized without that specific dynamic property, or if the code attempts to access a property that was never defined. While PHP allows you to define properties dynamically at runtime (e.g., $user->newField = 'value'), there is a hard restriction on reading them.

If you try to read a property via dynamic syntax that has never been assigned or defined, PHP throws the fatal "Cannot access empty property" error. This is a safety mechanism preventing undefined behavior when accessing non-existent keys through variables.

The Safe Alternative: property_exists and isset

Because of this restriction, you cannot simply use $this->$var to check for existence; you will hit the fatal error before PHP even gets to a standard "undefined property" notice. To safely handle dynamic properties, you must explicitly verify their existence first.

The most robust approach is using property_exists():

$targetProp = 'name';

if (property_exists($this, $targetProp)) {
    echo $this->$targetProp;
} else {
    // Handle the missing property gracefully
    echo "Property '$targetProp' does not exist.";
}

Using isset() with dynamic properties is tricky. If you run isset($this->$var) where $var references a non-existent property, PHP throws the same fatal error before isset can evaluate to false. You must always wrap the access in a check that validates the property's presence before attempting the read.

Practical Application in Laravel

If you are working within the Laravel ecosystem, this distinction becomes critical when building dynamic form fields or rendering views based on model attributes. For instance, if you have a JSON response where keys are dynamic, you cannot blindly access them using $model->$key.

Laravel provides utility methods that abstract these complexities. While you can manipulate raw properties as shown above, relying on get() or accessing the underlying data via arrays ($model->getAttribute($key)) is often safer and more predictable when dealing with dynamic inputs.

For deeper dives into handling dynamic attributes safely in modern PHP applications, resources like https://laravelcompany.com offer comprehensive guides on best practices for object interaction.

Final Considerations

The takeaway is clear: $this->$var is powerful but dangerous if not guarded. It assumes the property exists. If it doesn't, PHP halts execution with a fatal error rather than treating it as a simple undefined variable. Always validate dynamic property names against the object's scope before attempting to read them using the curly-brace syntax.

Our network