Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

miwal's avatar
Level 11

When to use php serialize() vs. laravel's json serialization - how to decide

Is it correct to say that while php does have it's own serialize() function which produces its own form of serialised objects & arrays, e.g., in tinker:

>>> $arr = [1, 2, [ 3, 4]]
=> [
     1,
     2,
     [
       3,
       4,
     ],
   ]
>>> serialize($arr)
=> "a:3:{i:0;i:1;i:1;i:2;i:2;a:2:{i:0;i:3;i:1;i:4;}}"

that laravel in fact doesn't use that, but in fact when it talks about 'serializing' it instead always(?) converts to json, since that's the more popular format at this point. Just wanted to get that ambiguity clear in my head - have I got this correct? thanks.

Are the formats "equivalent" in terms of usefulness?

.... it looks like while setting $casts to 'array' in a model serializes to json, but session entries (in my case in Redis) use php serialization instead, in the form given above. From what I read online, is that because it's easer to get classes back from php serialize than json?

So, is the thing to know that we should expect to see both in the wild at this point, & choice of which isn't always definitive even in same/similar use case?

What is the heuristic we should use to make the choice?

Is it something like, "use json, unless it's a big datastructure and you want to serialize classes?"

0 likes
0 replies

Please or to participate in this conversation.