3

I want to cache an array which has also closures.

I've tried with:

  • serialize() - can't serialize closures
  • json_encode() - replaces closure with empty value
  • base64_encode() - doesn't accept arrays

What else to do for caching array containing also closures ?

  • But closure serialization is not allowed by php "from the box". You can only use 3rd party libraries to reach your purpose. https://www.php.net/manual/ru/function.serialize.php#113305 – Viktor Jun 30 '19 at 10:49
  • There is no native way to serialize closures. https://stackoverflow.com/questions/13734224/exception-serialization-of-closure-is-not-allowed/19730234#19730234 –  Jun 30 '19 at 10:52

1 Answers1

2

Closures in PHP can not be serialized "from the box" hence array with closures too. You can only use 3rd party ways/libraries to reach this. Discussed here: Serializing anonymous functions in php

Viktor
  • 188
  • 7