Carbon: Exception: Unknown getter 'locale'
Hello,
I encountered an issue with the following code:
$holidays = ['0' => '2020-12-25', '1' => '2020-12-24']; //etc..
return $start_date->diffInDaysFiltered(function (Carbon $d) use ($holidays) {
return $d->isWeekday() && !array_search($d->format("Y-m-d"), $holidays,true);
}, $end_date);
Carbon version: 2.41.0
PHP version: 7.3.5
I expected to get:
number of diff days between start_date and end_date
But I actually get:
Exception: Unknown getter 'locale'
With version 2.40.1 work!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 25
Commits related to this issue
- Proposed change for fixing #2217 Well, if using `property_exists` before trying to acces prop, we can assure that it exists and no exception is triggered. Closes #2217 — committed to mllull/Carbon by mllull 3 years ago
- Fix #2217 Implement get() method on CarbonPeriod Use a debugger-friendly (with no try-catch) __isset() tester — committed to briannesbitt/Carbon by kylekatarnls 3 years ago
You can try to pull the last commit on
"nesbot/carbon": "dev-fix/issue-2217-mute-getter"
, I refactored to not use exception as it’s a good thing anyway in this case. So you should not longer have your debugger pausing with this change.But:
We can’t consider this as an issue. Throwing an exception is not a bug, it tells “You can’t do that”.
This is not an anomaly. Here this is an expected exception. And try-catch is an existing PHP tool, so you should not expect all your included libraries not to use it. That’s meant for what its name literally describe: to try something.
Try to get “locale” => “You can’t do that” => OK, not a problem I have an alternative solution. And as long as it’s specific scoped exception types, this is actually library business user-land tools should not care about/interfere with.
So Carbon will fix this particular case on the next version, but be warned, similar caught exceptions may legitimately occur from other places and also from many other libraries we/you may include.