How to test if data is serialized php

Very weird problem in the PHP world is testing if data is unserializable, because PHP throws a notice which passes the try…catch block and can for example stop Symfony commands. I saw a lot of very complicated solutions which analyze the strings and does very complicated logic to figure out if the string can successfully unserialize.  Kudos to the authors of those, I however wanted a simpler solution so I set a custom error handler in my function which catches the E_NOTICE and E_WARNING and throws an exception, then catch the exception if it exists and restore the handler. Get it? Got it? Good. Now lets see the actual code.

 

 

About Pavel Petrov 2 Articles |  18 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.


*