briannesbitt/Carbon · error · UnknownMethodException
Method $method does not exist.
Error message
Method $method does not exist.
What it means
CarbonPeriod::__call() maps fluent unit names ('days', 'hours', 'month', singulars, etc.) to interval setters and forwards registered macros. For anything unrecognized, behavior depends on strict mode (per-period ->setStrictMode() or the date class global setting): strict mode throws UnknownMethodException ('Method X does not exist.'), non-strict mode silently returns $this and swallows the typo (src/Carbon/CarbonPeriod.php:1900).
Source
Thrown at src/Carbon/CarbonPeriod.php:1900
case 'hour':
case 'minutes':
case 'minute':
case 'seconds':
case 'second':
case 'milliseconds':
case 'millisecond':
case 'microseconds':
case 'microsecond':
return $this->setDateInterval((
// Override default P1D when instantiating via fluent setters.
[$this->isDefaultInterval ? new CarbonInterval('PT0S') : $this->dateInterval, $method]
)(...$parameters));
}
$dateClass = $this->dateClass;
if ($this->localStrictModeEnabled ?? $dateClass::isStrictModeEnabled()) {
throw new UnknownMethodException($method);
}
return $this;
}
/**
* Set the instance's timezone from a string or object and apply it to start/end.
*/
public function setTimezone(DateTimeZone|string|int $timezone): static
{
$self = $this->copyIfImmutable();
$self->timezoneSetting = $timezone;
$self->timezone = CarbonTimeZone::instance($timezone);
if ($self->startDate) {
$self = $self->setStartDate($self->startDate->setTimezone($timezone));
}
View on GitHub (pinned to b13f05955d)
Solutions
- Fix the method name — check the real API: setStartDate(), setEndDate(), setDateInterval(), setRecurrences(), filter(), excludeStartDate()
- Register the macro before use: CarbonPeriod::macro('myMethod', fn (...) => ...)
- Verify dynamically: method_exists($period, $name) || CarbonPeriod::hasMacro($name)
Example fix
// before
$period = CarbonPeriod::create()->startdDate('2021-01-01'); // typo
// after
$period = CarbonPeriod::create()->setStartDate('2021-01-01'); Defensive patterns
Strategy: type-guard
Type guard
function periodSupports(CarbonPeriod $period, string $method): bool
{
return method_exists($period, $method) || CarbonPeriod::hasMacro($method);
}
if (!periodSupports($period, $name)) {
throw new BadMethodCallException("CarbonPeriod has no method '$name'");
}
$period->{$name}(...$args); Try / catch
try {
$result = $period->{$dynamicName}();
} catch (\Carbon\Exceptions\UnknownMethodException $e) {
// log the typo'd/misrouted call with the method name from $e->getMethod()
throw $e;
} Prevention
- Keep strict mode on (default) so typos fail loudly instead of returning $this silently
- Register macros at bootstrap, not lazily, and verify with CarbonPeriod::hasMacro()
- Add static analysis (PHPStan) to catch misspelled fluent calls before runtime
When it happens
Trigger: Typos in fluent chains: ->startdDate('...') or ->setDateIntervall('P1D'); calling date methods on the period (->addDay()); calling a macro name that was never registered with CarbonPeriod::macro() — all while strict mode is enabled (the default).
Common situations: Long fluent builder chains where one segment is misspelled and fails only at runtime; upgrading code that relied on non-strict silent no-ops; macros renamed or moved between package versions.
Related errors
- Argument 1 passed to {$class}::{$method}() must be an instan
- Invalid ISO 8601 specification: {$iso}.
- $anchorDay parameter must not be set for $mode OverflowMode:
- You must specify $end or $recurrences but not both
- Invalid constructor parameters.
AI-assisted analysis of briannesbitt/Carbon@b13f05955d (2026-08-17).
Data as JSON: /api/errors/a408879034ebd5ce.
Report an issue: GitHub.