{"id":373,"date":"2011-11-03T17:16:00","date_gmt":"2011-11-03T17:16:00","guid":{"rendered":""},"modified":"2013-02-07T12:10:29","modified_gmt":"2013-02-07T04:10:29","slug":"php%3a-serialize%e5%92%8cunserialize","status":"publish","type":"post","link":"https:\/\/ybzx.vip\/wp\/?p=373","title":{"rendered":"PHP: serialize\u548cunserialize"},"content":{"rendered":"<p>serialize()\u548cunserialize()\u5728php\u624b\u518c\u4e0a\u7684\u89e3\u91ca\u662f: <br \/>serialize \u2014 Generates a storable representation of a value <br \/>serialize \u2014 \u4ea7\u751f\u4e00\u4e2a\u53ef\u5b58\u50a8\u7684\u503c\u7684\u8868\u793a <br \/>unserialize \u2014 Creates a PHP value from a stored representation <br \/>unserialize \u2014 \u4ece\u5df2\u5b58\u50a8\u7684\u8868\u793a\u4e2d\u521b\u5efa PHP \u7684\u503c <br \/>serialize <br \/>(PHP 3>= 3.0.5, PHP 4 ) <br \/>serialize &#8212;&nbsp;&nbsp;\u4ea7\u751f\u4e00\u4e2a\u53ef\u5b58\u50a8\u7684\u503c\u7684\u8868\u793a <br \/>\u63cf\u8ff0string serialize ( mixed value) <br \/>serialize() \u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u6b64\u5b57\u7b26\u4e32\u5305\u542b\u4e86\u8868\u793a value \u7684\u5b57\u8282\u6d41\uff0c\u53ef\u4ee5\u5b58\u50a8\u4e8e\u4efb\u4f55\u5730\u65b9\u3002 <br \/>\u8fd9\u6709\u5229\u4e8e\u5b58\u50a8\u6216\u4f20\u9012 PHP \u7684\u503c\uff0c\u540c\u65f6\u4e0d\u4e22\u5931\u5176\u7c7b\u578b\u548c\u7ed3\u6784\u3002 <br \/>\u60f3\u8981\u5c06\u5df2\u5e8f\u5217\u5316\u7684\u5b57\u7b26\u4e32\u53d8\u56de PHP \u7684\u503c\uff0c\u53ef\u4f7f\u7528 unserialize()\u3002 <br \/>serialize() \u53ef\u5904\u7406\u9664\u4e86 resource \u4e4b\u5916\u7684\u4efb\u4f55\u7c7b\u578b\u3002 <br \/>\u751a\u81f3\u53ef\u4ee5 serialize() \u90a3\u4e9b\u5305\u542b\u4e86\u6307\u5411\u5176\u81ea\u8eab\u5f15\u7528\u7684\u6570\u7ec4\u3002 <br \/>\u4f60\u6b63 serialize() \u7684\u6570\u7ec4\uff0f\u5bf9\u8c61\u4e2d\u7684\u5f15\u7528\u4e5f\u5c06\u88ab\u5b58\u50a8\u3002 <br \/>\u5f53\u5e8f\u5217\u5316\u5bf9\u8c61\u65f6\uff0cPHP \u5c06\u8bd5\u56fe\u5728\u5e8f\u5217\u52a8\u4f5c\u4e4b\u524d\u8c03\u7528\u8be5\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570 __sleep()\u3002\u8fd9\u6837\u5c31\u5141\u8bb8\u5bf9\u8c61\u5728\u88ab\u5e8f\u5217\u5316\u4e4b\u524d\u505a\u4efb\u4f55\u6e05\u9664\u64cd\u4f5c\u3002\u7c7b\u4f3c\u7684\uff0c\u5f53\u4f7f\u7528 unserialize() \u6062\u590d\u5bf9\u8c61\u65f6\uff0c \u5c06\u8c03\u7528 __wakeup() \u6210\u5458\u51fd\u6570\u3002 <br \/>\u6ce8: \u5728 PHP 3 \u4e2d\uff0c\u5bf9\u8c61\u5c5e\u6027\u5c06\u88ab\u5e8f\u5217\u5316\uff0c\u4f46\u662f\u65b9\u6cd5\u5219\u4f1a\u4e22\u5931\u3002PHP 4 \u6253\u7834\u4e86\u6b64\u9650\u5236\uff0c\u53ef\u4ee5\u540c\u65f6\u5b58\u50a8\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8bf7\u53c2\u89c1\u7c7b\u4e0e\u5bf9\u8c61\u4e2d\u7684\u5e8f\u5217\u5316\u5bf9\u8c61\u90e8\u5206\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002 <br \/>\u4e0a\u4f8b\u5b50 <br \/>$array = array(); <br \/>$array[\u201dkey\u201d] = \u201ckey\u201d; <br \/>$array[\u201dvalue\u201d]=\u201dvalue\u201d; <br \/>echo serialize($array); <br \/>\u7ed3\u679c\uff1aa:2:&#123;s:3:\u201dkey\u201d;s:3:\u201dkey\u201d;s:5:\u201dvalue\u201d;s:5:\u201dvalue\u201d;&#125; <br \/>serialize &#8212;&nbsp;&nbsp;\u4ea7\u751f\u4e00\u4e2a\u53ef\u5b58\u50a8\u7684\u503c\u7684\u8868\u793a <!--more--><\/p>\n<p>\u518d\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php \r\n\/\/\u58f0\u660e\u4e00\u4e2a\u7c7b \r\nclass dog &amp;#123; \r\n\r\nvar $name; \r\nvar $age; \r\nvar $owner; \r\n\r\nfunction dog($in_name=&quot;unnamed&quot;,$in_age=&quot;0&quot;,$in_owner=&quot;unknown&quot;) &amp;#123; \r\n$this-&gt;name = $in_name; \r\n$this-&gt;age = $in_age; \r\n$this-&gt;owner = $in_owner; \r\n&amp;#125; \r\n\r\nfunction getage() &amp;#123; \r\nreturn ($this-&gt;age * 365); \r\n&amp;#125; \r\n\r\nfunction getowner() &amp;#123; \r\nreturn ($this-&gt;owner); \r\n&amp;#125; \r\n\r\nfunction getname() &amp;#123; \r\nreturn ($this-&gt;name); \r\n&amp;#125; \r\n&amp;#125; \r\n\/\/\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u7c7b \r\n$ourfirstdog = new dog(&quot;Rover&quot;,12,&quot;Lisa and Graham&quot;); \r\n\/\/\u7528serialize\u51fd\u6570\u5c06\u8fd9\u4e2a\u5b9e\u4f8b\u8f6c\u5316\u4e3a\u4e00\u4e2a\u5e8f\u5217\u5316\u7684\u5b57\u7b26\u4e32 \r\n$dogdisc = serialize($ourfirstdog); \r\nprint $dogdisc; \/\/$ourfirstdog \u5df2\u7ecf\u5e8f\u5217\u5316\u4e3a\u5b57\u7b26\u4e32 O:3:&quot;dog&quot;:3:&amp;#123;s:4:&quot;name&quot;;s:5:&quot;Rover&quot;;s:3:&quot;age&quot;;i:12;s:5:&quot;owner&quot;;s:15:&quot;Lisa and Graham&quot;;&amp;#125; \r\n\r\nprint &amp;#039;&lt;BR&gt;&amp;#039;; \r\n\r\n\/* \r\n----------------------------------------------------------------------------------------- \r\n\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32 $dogdisc \u5b58\u50a8\u5230\u4efb\u4f55\u5730\u65b9\u5982 session,cookie,\u6570\u636e\u5e93,php\u6587\u4ef6 \r\n----------------------------------------------------------------------------------------- \r\n*\/ \r\n\r\n\/\/\u6211\u4eec\u5728\u6b64\u6ce8\u9500\u8fd9\u4e2a\u7c7b \r\nunset($ourfirstdog); \r\n\r\n\/* \u8fd8\u539f\u64cd\u4f5c *\/ \r\n\r\n\/* \r\n----------------------------------------------------------------------------------------- \r\n\u5728\u8fd9\u91cc\u5c06\u5b57\u7b26\u4e32 $dogdisc \u4ece\u4f60\u5b58\u50a8\u7684\u5730\u65b9\u8bfb\u51fa\u6765\u5982 session,cookie,\u6570\u636e\u5e93,php\u6587\u4ef6 \r\n----------------------------------------------------------------------------------------- \r\n*\/ \r\n\r\n\/\/\u6211\u4eec\u5728\u8fd9\u91cc\u7528 unserialize() \u8fd8\u539f\u5df2\u7ecf\u5e8f\u5217\u5316\u7684\u5bf9\u8c61 \r\n$pet = unserialize($dogdisc); \/\/\u6b64\u65f6\u7684 $pet \u5df2\u7ecf\u662f\u524d\u9762\u7684 $ourfirstdog \u5bf9\u8c61\u4e86 \r\n\/\/\u83b7\u5f97\u5e74\u9f84\u548c\u540d\u5b57\u5c5e\u6027 \r\n$old = $pet-&gt;getage(); \r\n$name = $pet-&gt;getname(); \r\n\/\/\u8fd9\u4e2a\u7c7b\u6b64\u65f6\u65e0\u9700\u5b9e\u4f8b\u5316\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528,\u800c\u4e14\u5c5e\u6027\u548c\u503c\u90fd\u662f\u4fdd\u6301\u5728\u5e8f\u5217\u5316\u4e4b\u524d\u7684\u72b6\u6001 \r\nprint &quot;Our first dog is called $name and is $old days old&lt;br&gt;&quot;; \r\nprint &amp;#039;&lt;BR&gt;&amp;#039;; \r\n?&gt; \r\n<\/pre>\n<p>\u6587\u7ae0\u6765\u6e90\uff1ahttp:\/\/www.phphubei.com\/thread-104-1-3.html <\/p>\n","protected":false},"excerpt":{"rendered":"<p>serialize()\u548cunserialize()\u5728php\u624b\u518c\u4e0a\u7684\u89e3\u91ca\u662f: serialize \u2014 Gener &hellip; <a href=\"https:\/\/ybzx.vip\/wp\/?p=373\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">PHP: serialize\u548cunserialize<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[97],"class_list":["post-373","post","type-post","status-publish","format-standard","hentry","category-network","tag-php"],"_links":{"self":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/posts\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=373"}],"version-history":[{"count":0,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/posts\/373\/revisions"}],"wp:attachment":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}