A PDO object contains state that cannot be represented in the serialization format. For example, the PDO object contains an open connection to a database server.
If you were to try to deserialize a serialized PDO object, the __wakeup()
method would have to reconnect to the database server. This would require that authentication credentials be stored in a readable manner in the serialized PDO object, which is a security no-no.
I worked on the Zend Framework's Zend_Db component a long time ago, and I deliberately designed the Zend_Db_Adapter object to not be serializable for this reason. Instances of Zend_Db_Table, Zend_Db_Table_Row, etc. could be serializable, but could not be "live" after deserialization until you assigned it a freshly connected Zend_Db_Adapter instance.
Also, there's no guarantee that the database server would be reachable at the time you deserialize the PDO object. It's unclear whether this means the deserialization would be considered "failed."
The same restrictions on serialization apply to other resources such as sockets or file handles.
See also Why isn't every type of object serializable?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…