yiisoft/yii2 · error · UnknownPropertyException

Getting unknown property: {class}::{name}

Error message

Getting unknown property: {class}::{name}

What it means

BaseObject::__get() throws UnknownPropertyException('Getting unknown property: C::p') when the accessed name has no getter, no setter, and is not a public member variable - that property simply does not exist on the object. The message names the concrete class, so check exactly that class's property list; magic properties are documented via @property annotations in the class docblock.

Source

Thrown at framework/base/BaseObject.php:142

     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `$value = $object->property;`.
     * @param string $name the property name
     * @return mixed the property value
     * @throws UnknownPropertyException if the property is not defined
     * @throws InvalidCallException if the property is write-only
     * @see __set()
     */
    public function __get($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        } elseif (method_exists($this, 'set' . $name)) {
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
        }

        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
    }

    /**
     * Sets value of an object property.
     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `$object->property = $value;`.
     * @param string $name the property name or the event name
     * @param mixed $value the property value
     * @throws UnknownPropertyException if the property is not defined
     * @throws InvalidCallException if the property is read-only
     * @see __get()
     */
    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Fix the property name against the class's @property annotations or getter list
  2. If the value may be absent, guard the read with $object->canGetProperty($name) or isset($object->$name)
  3. If the property should exist on your class, add the getter or a public field
  4. Check the installed version (composer show vendor/package) against the docs you followed

Example fix

// before
$email = $user->emial; // typo

// after
$email = $user->email;
Defensive patterns

Strategy: type-guard

Validate before calling

if ($model->canGetProperty('email')) {
    $email = $model->email;
} else {
    $email = ''; // property absent on this object/version
}

Type guard

function canRead(\yii\base\BaseObject $object, string $name): bool
{
    return isset($object->$name) || $object->canGetProperty($name);
}

Try / catch

try {
    $value = $object->$name;
} catch (\yii\base\UnknownPropertyException $e) {
    // message names the class and the missing property
    Yii::warning($e->getMessage());
    $value = $default;
}

Prevention

When it happens

Trigger: Typo on a read path ($user->emial); reading a property that exists only on a subclass while the variable holds a base-class or sibling instance; accessing a protected/private member from outside; reading a magic property that the installed package version does not have yet (added in a later release).

Common situations: Version drift after composer update or downgrade (properties renamed or moved between classes); templates and generic loops reading object fields dynamically; refactors that moved a getter to a helper class.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/fdcb0807bd348bc1. Report an issue: GitHub.