How to serialize object that has closures inside properties?

后端 未结 4 1335
面向向阳花
面向向阳花 2021-01-14 05:57

if I do serialize($obj), I get:

Serialization of \'Closure\' is not allowed

Is there any way these closures can be

4条回答
  •  半阙折子戏
    2021-01-14 06:32

    It's simple: You can't. A closure is not serializable. If you want to create "something similar", you can use objects of a class implementing __invoke() instead of the closures.

    If you don't need the closures (or you can recreate them yourself while unserializing) you can implement Serializable and don't take the closures into account while serializing. This interface (implementing serialize() and unserialize()) should be prefered over __sleep()/__wakeup().

提交回复
热议问题