How to serialize an exception with closure

5

I'm trying to serialize the exceptions generated by errors to write a log with all the information, but the problem is that some exceptions may come with a closure within the trace and trigger the message below:

  Uncaught exception 'Exception' with message 'Serialization of' Closure 'is not allowed'

If I write a file using the object $exception it writes using the magic method __toString . I wanted to write the complete exception , so I use serialize / unserialize .

From what I saw, a closure object can not be serialized, so how can I remove it from the trace exception and serialize it? My final wish is to use OB* for this.

    
asked by anonymous 17.11.2014 / 19:16

1 answer

7

If this is not a problem, you can include a package in your project to enable serialization of Clousures .

I remember that Laravel 4 uses this same package for the same problem:

link

Removed from the readme of Super Closure:

  

Have you ever seen this?

Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed'
     

It's true! If you try to serialize the Closure, PHP will throw an   exception and tell you that it is not allowed. But even though it is   not "allowed" by PHP, the Super Closure library   (jeremeamia / superclosure on Packagist) makes it possible.

Free translation:

  

Have you ever seen this?

Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed'
     

Yes, it is true! If you try to serialize a Closure, PHP will throw   an exception stating that it is not possible. But even though that   is not "allowed" by PHP, the Super Closure library   (jeremeamia / superclosure in Packagist) makes this possible.

    
17.11.2014 / 20:00