1

In the project that I am working on (PHP 7, Slim Framework), there is a service class that needs alot of time to get instanciated. In order to improve the overall performance of the system I though that an implementation as a singleton would help. I decided to use apcu in order to store the class instance and then fetch whenever needed.

My code for adding the instance to the cache is

$config = [ .... ];
if (!apcu_exists("mediaService")) {
     $mediaService = new \Services\MediaService($config);
     apcu_add("mediaService", $mediaService);
}

When I run it PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed.

Any thoughts?

  • 1
    Is the error message not clear? The object you're attempting to store contains a Closure [an anonymous function] that cannot be serialized. [See the notes on `serialize()`](http://php.net/manual/en/function.serialize.php#refsect1-function.serialize-notes) – Sammitch Sep 19 '17 at 17:41
  • I get that, but I cannot work around this problem because internally I use a third-party library which is the one that causing it. Are there any apcu alternatives that don't require serialization? – user3371927 Sep 19 '17 at 19:25
  • There is not a caching library on earth that will not require serialization of one form or another, and you cannot serialize code. – Sammitch Sep 19 '17 at 20:22

0 Answers0