briannesbitt/Carbon · error · UnknownGetterException

$name

Error message

$name

What it means

CarbonPeriod exposes a small fixed set of magic properties through get()/__get(), mapped in getGetter() (src/Carbon/CarbonPeriod.php:859-878): start/start_date, end/end_date, interval/date_interval, recurrences, include_start_date, include_end_date, current, locale, tzname/tz_name — camelCase is converted to snake_case. Any other name raises UnknownGetterException with the property name as the message (src/Carbon/CarbonPeriod.php:895).

Source

Thrown at src/Carbon/CarbonPeriod.php:895

        };
    }

    /**
     * Get a property allowing both `DatePeriod` snakeCase and camelCase names.
     *
     * @param string $name
     *
     * @return bool|CarbonInterface|CarbonInterval|int|null
     */
    public function get(string $name)
    {
        $getter = $this->getGetter($name);

        if ($getter) {
            return $getter();
        }

        throw new UnknownGetterException($name);
    }

    /**
     * Get a property allowing both `DatePeriod` snakeCase and camelCase names.
     *
     * @param string $name
     *
     * @return bool|CarbonInterface|CarbonInterval|int|null
     */
    public function __get(string $name)
    {
        return $this->get($name);
    }

    /**
     * Check if an attribute exists on the object
     *
     * @param string $name

View on GitHub (pinned to b13f05955d)

Solutions

  1. Use the supported names or the real methods: getStartDate(), getEndDate(), getDateInterval(), getRecurrences(), isStartIncluded(), isEndIncluded()
  2. Guard dynamic access with isset($period->$name) — __isset() is wired to getGetter() and returns false instead of throwing
  3. Read non-magic data via real methods: ->getOptions(), ->getFilters(), ->getDateClass()

Example fix

// before
foreach (['start', 'end', 'options', 'filters'] as $key) {
    $data[$key] = $period->{$key}; // UnknownGetterException on 'options'/'filters'
}

// after
$data = [
    'start' => $period->getStartDate(),
    'end' => $period->getEndDate(),
    'options' => $period->getOptions(),
    'filters' => $period->getFilters(),
];
Defensive patterns

Strategy: type-guard

Type guard

$known = ['start', 'start_date', 'end', 'end_date', 'interval', 'date_interval',
    'recurrences', 'include_start_date', 'include_end_date', 'current', 'locale', 'tzname', 'tz_name'];

function canGet(CarbonPeriod $period, string $name): bool
{
    return isset($period->$name); // __isset() maps to getGetter(), never throws
}

$value = canGet($period, $field) ? $period->get($field) : null;

Try / catch

try {
    $value = $period->get($field);
} catch (\Carbon\Exceptions\UnknownGetterException $e) {
    $value = null; // or use a real getter: match($field) { 'options' => $period->getOptions(), ... }
}

Prevention

When it happens

Trigger: $period->get('options'); $period->filters; $period->date_class; $period->timezone (valid magic name is tzname); dynamic loops like $period->$field where $field comes from a key list containing unsupported names.

Common situations: Serializing/exporting periods by iterating a fixed key list; assuming DatePeriod-style property names that are not mapped (e.g. exclude_start_date); typos in camelCase access (startDate works, startdDate throws).

Related errors


AI-assisted analysis of briannesbitt/Carbon@b13f05955d (2026-08-17). Data as JSON: /api/errors/1623cba53dd7b4a0. Report an issue: GitHub.