I am making a multi-threaded CLI-PHP application and need to serialize PDO object
to pass it between work inside the thread, and wake it up from a sleeping thread using magic methods __sleep()
and __wakeup()
. However nor the PDO
or mysqli
extension supports it. The old mysql_*()
api did this but it has been deprecated and removed.
<?php
// Application
$link = new PDO('mysql:host=localhost;port=3306;dbname=testdatabase', 'root', '');
$obj = serialize($link);
well generate error
See Question&Answers more detail:osPHP Fatal error: Uncaught exception 'PDOException' with message 'You cannot ser ialize or unserialize PDO instances' in W:workspaceSandboxapplication.php:5 Stack trace:
#0 [internal function]: PDO->__sleep()
#1 W:workspaceSandboxapplication.php(5): serialize(Object(PDO))
#2 {main} thrown in W:workspaceSandboxapplication.php on line 5