serialize unserialize

来源:互联网 发布:阿里云ecs搭建淘宝客 编辑:程序博客网 时间:2024/05/16 17:08
当序列化对象时,PHP 将试图在序列动作之前调用该对象的成员函数 __sleep()。这样就允许对象在被序列化之前做任何清除操作。类似的,当使用 unserialize() 恢复对象时, 将调用 __wakeup() 成员函数。
<?/*Anatomy of a serialize()'ed value:Strings:size:value;Integeri:value;Decimal     (float-value)d:valueBooleanb:value; (does not store "true" or "false", does store '1' or '0')NullN;Arraya:size:{key definition;value definition;(repeated per element)}ObjectO:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)}String values are always in double quotesArray keys are always integers or strings    "null => 'value'" equates to 's:0:"";s:5:"value";',    "true => 'value'" equates to 'i:1;s:5:"value";',    "false => 'value'" equates to 'i:0;s:5:"value";',    "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an    array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";',     and    attempting to use an object as a key will result in the same behavior as using an array will.*/?>
序列化的问题: 
1.Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query manipulate data to be inserted. I've encountered this too many times in my career, it makes for difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized. That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare.2.Serializing floating point numbers leads to weird precision offset errors:<?phpecho round(96.670000000000002, 2);// 96.67echo serialize(round(96.670000000000002, 2));// d:96.670000000000002;echo serialize(96.67);// d:96.670000000000002;---------------------------------------------------------------------------------$ php -r "var_dump( unserialize( serialize(round(96.670000000000002, 2)) ) );"float(96.67)?>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Not only is this wrong, but it adds a lot of unnecessary bulk to serialized data.Probably better to use  json_encode() instead (which apparently is faster than serialize(), anyway).+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0 0