"PHP Notice: Undefined property"

"PHP Notice: Undefined property"

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Notice: Undefined property

You encounter a classic "Undefined property" notice in your logs, and your immediate instinct is to double-check your code. You verify that the property exists on the object, you confirm it's initialized, and yet the error persists. It feels strange because logic dictates that if the property is there, PHP shouldn't complain about its absence.

The culprit in this scenario isn't a missing variable; it is a specific interaction between protected properties, magic __get() methods, and how PHP handles uninitialized objects. This usually happens when you accidentally access a property on an object that hasn't been instantiated yet—specifically, a stdClass instance created implicitly by PHP to handle the null reference.

Consider this scenario where you are checking user roles:

$canWrite = $this->session->isLoggedIn() 
    ? $this->page->canWrite($this->session->user->roles) 
    : false;

You might have a User class that looks like this:

class User {
    protected $roles;

    function getRoles() {
        if (!$this->roles) {
            // Load roles from DB
            $this->roles = fetchFromDatabase();
        }
        return $this->roles;
    }
}

Even though getRoles() is defined, accessing $user->roles directly inside a magic getter chain can trigger a notice if the object instance itself is flawed. In your specific case, the issue stems from an earlier line of code:

$this->session->user->id = $user->id;

If $this->session->user does not exist yet (i.e., it is null), PHP attempts to access the id property on that null value. To prevent a fatal error, PHP silently creates a new stdClass object and attaches the id property to it. However, this "ghost" object retains no connection to your actual User class logic. When you subsequently try to access $this->session->user->roles, you are accessing a property on that unintended stdClass instance. Since stdClass does not define roles, and there is no valid magic method fallback for a standard public property access in this specific implicit context, PHP throws the notice.

The confusion often arises because of how magic methods interact with these implicit objects. You might have implemented __get() to handle lazy loading:

public function __get($var) {
    if ($this->__isset($var)) {
        $method = 'get'.ucfirst($var);
        if (method_exists($this, $method)) {
            return $this->$method();
        } else {
            return $this->$var;
        }
    }
    throw new Exception("Unrecognized attribute '$name'");
}

While this logic is sound for a properly instantiated User object (as confirmed by your var_dump() showing the correct structure with roles populated), it does not save you from the initial notice generated by the stdClass fallback. The magic method is never called on the implicit stdClass because the property access $user->id happens before the class context is established, or PHP treats the uninitialized state differently than a standard class instance.

The fix is straightforward: ensure that object instantiation occurs before any property access. Refactor your code to initialize the user object explicitly:

// Force initialization before accessing properties
if (!$this->session->user) {
    $this->session->user = new User();
}

By ensuring $this->session->user is a valid instance of User before you attempt to assign id or read roles, you eliminate the stdClass ghost object. This guarantees that subsequent property accesses trigger your intended magic methods and logic, keeping your error logs clean and your application behavior predictable.

Our network