briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.57k stars 1.28k forks source link

Serialization of 'Closure' is not allowed #3002

Closed alvaronds closed 6 months ago

alvaronds commented 6 months ago

Hello,

I encountered an issue with the following code:

If I try to serializate a CarbonInterval I can without problems:

$a = new CarbonInterval(1);
$b = serialize($a);

But if the CarbonInterval has been generated with diffAsCarbonInterval I get an "Serialization of 'Closure' is not allowed" error.

$a = Carbon::now();
$b = Carbon::now()->addDay();
$c = $a->diffAsCarbonInterval($b);
$d = serialize($c);

I also have problems unserializing saved CarbonIntervals generated with Carbon 2 in Laravel. I need to do this to unserialize them:

str_replace('tzName','timezoneSetting',$mySavedCarbonInterval);

Carbon version: 3.3.0

PHP version: 8.3.4

I expected to get:

The serialized CarbonInterval

But I actually get:

PHP ERROR: Serialization of 'Closure' is not allowed

I tried the code in the https://play.phpsandbox.io and fails with Carbon 3. Carbon 2 works great

Thanks!

kylekatarnls commented 6 months ago

Serialization of CarbonInterval is now possible in 3.3.1.

kylekatarnls commented 6 months ago

Unserialization of Carbon v2 interval will be handled in a separated issue: #3015