PHP Serialize Adding Control Characters to Object

2020-04-13 07:10发布

问题:

I've got a PHP object which I am serializing and storing in a mySQL longText field. The mySQL database is encoded in UTF-8 and so is this longText field. When I use a mySQL client to copy the contents of the longText field, and paste them into a text editor, I find that control characters have been inserted in the property names belonging to the object. Here's a screen shot, with the control characters shown in red:

Is this normal for php serialize?

回答1:

Presuming the ¿ in your example stands for a null byte, then yes it is normal, per the manual:

Note: Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side. ~ PHP Manual: Serialize

...which is something to be aware of when you move serialized objects around. The loss of those null-bytes would render your object unserializable (or you might get odd-named all-public properties).