PHP ÐòÁл¯£¨serialize£©¸ñʽÏê½â(2)
4£®¼òµ¥¸´ºÏÀàÐ͵ÄÐòÁл¯
PHP Öеĸ´ºÏÀàÐÍÓÐÊý×飨array£©ºÍ¶ÔÏó£¨object£©Á½ÖÖ£¬±¾ÕÂÖ÷Òª½éÉÜÔÚ¼òµ¥Çé¿öÏÂÕâÁ½ÖÖÀàÐÍÊý¾ÝµÄÐòÁл¯¸ñʽ¡£¹ØÓÚǶÌ׶¨ÒåµÄ¸´ºÏÀàÐͺÍ×Ô¶¨ÒåÐòÁл¯·½Ê½µÄ¶ÔÏóµÄÐòÁл¯¸ñʽ½«ÔÚºóÃæµÄÕ½ÚÏêϸÌÖÂÛ¡£
4.1£®Êý×éµÄÐòÁл¯
Êý×飨array£©Í¨³£±»ÐòÁл¯Îª£º
a:<n>:{<key 1><value 1><key 2><value 2>...<key n><value n>}ÆäÖÐ <n> ±íʾÊý×éÔªËصĸöÊý£¬<key 1>¡¢<key 2>¡¡<key n> ±íʾÊý×éϱ꣬<value 1>¡¢<value 2>¡¡<value n> ±íʾÓëϱêÏà¶ÔÓ¦µÄÊý×éÔªËصÄÖµ¡£
ϱêµÄÀàÐÍÖ»ÄÜÊÇÕûÐÍ»òÕß×Ö·û´®ÐÍ£¨°üÀ¨ºóÃæÄÇÖÖ Unicode ×Ö·û´®ÐÍ£©£¬ÐòÁл¯ºóµÄ¸ñʽ¸úÕûÐͺÍ×Ö·û´®ÐÍÊý¾ÝÐòÁл¯ºóµÄ¸ñʽÏàͬ¡£
Êý×éÔªËØÖµ¿ÉÒÔÊÇÈÎÒâÀàÐÍ£¬ÆäÐòÁл¯ºóµÄ¸ñʽÓëÆäËù¶ÔÓ¦µÄÀàÐÍÐòÁл¯ºóµÄ¸ñʽÏàͬ¡£
4.2£®¶ÔÏóµÄÐòÁл¯
¶ÔÏó£¨object£©Í¨³£±»ÐòÁл¯Îª£º
O:<length>:"<class name>":<n>:{<field name 1><field value 1><field name 2><field value 2>...<field name n><field value n>}ÆäÖÐ <length> ±íʾ¶ÔÏóµÄÀàÃû <class name> µÄ×Ö·û´®³¤¶È¡£<n> ±íʾ¶ÔÏóÖеÄ×Ö¶Î1¸öÊý¡£ÕâЩ×ֶΰüÀ¨ÔÚ¶ÔÏóËùÔÚÀ༰Æä×æÏÈÀàÖÐÓà var¡¢public¡¢protected ºÍ private ÉùÃ÷µÄ×ֶΣ¬µ«ÊDz»°üÀ¨ static ºÍ const ÉùÃ÷µÄ¾²Ì¬×ֶΡ£Ò²¾ÍÊÇ˵ֻÓÐʵÀý£¨instance£©×ֶΡ£
<filed name 1>¡¢<filed name 2>¡¡<filed name n>±íʾÿ¸ö×ֶεÄ×Ö¶ÎÃû£¬¶ø <filed value 1>¡¢<filed value 2>¡¡<filed value n> Ôò±íʾÓë×Ö¶ÎÃûËù¶ÔÓ¦µÄ×Ö¶ÎÖµ¡£
×Ö¶ÎÃûÊÇ×Ö·û´®ÐÍ£¬ÐòÁл¯ºó¸ñʽÓë×Ö·û´®ÐÍÊý¾ÝÐòÁл¯ºóµÄ¸ñʽÏàͬ¡£
×Ö¶ÎÖµ¿ÉÒÔÊÇÈÎÒâÀàÐÍ£¬ÆäÐòÁл¯ºóµÄ¸ñʽÓëÆäËù¶ÔÓ¦µÄÀàÐÍÐòÁл¯ºóµÄ¸ñʽÏàͬ¡£
µ«×Ö¶ÎÃûµÄÐòÁл¯ÓëËüÃÇÉùÃ÷µÄ¿É¼ûÐÔÊÇÓйصģ¬ÏÂÃæÖصãÌÖÂÛһϹØÓÚ×Ö¶ÎÃûµÄÐòÁл¯¡£
4.3£®¶ÔÏó×Ö¶ÎÃûµÄÐòÁл¯
var ºÍ public ÉùÃ÷µÄ×ֶζ¼Êǹ«¹²×ֶΣ¬Òò´ËËüÃǵÄ×Ö¶ÎÃûµÄÐòÁл¯¸ñʽÊÇÏàͬµÄ¡£¹«¹²×ֶεÄ×Ö¶ÎÃû°´ÕÕÉùÃ÷ʱµÄ×Ö¶ÎÃû½øÐÐÐòÁл¯£¬µ«ÐòÁл¯ºóµÄ×Ö¶ÎÃûÖв»°üÀ¨ÉùÃ÷ʱµÄ±äÁ¿Ç°×º·ûºÅ $¡£
protected ÉùÃ÷µÄ×Ö¶ÎΪ±£»¤×ֶΣ¬ÔÚËùÉùÃ÷µÄÀàºÍ¸ÃÀàµÄ×ÓÀàÖпɼû£¬µ«ÔÚ¸ÃÀàµÄ¶ÔÏóʵÀýÖв»¿É¼û¡£Òò´Ë±£»¤×ֶεÄ×Ö¶ÎÃûÔÚÐòÁл¯Ê±£¬×Ö¶ÎÃûÇ°Ãæ»á¼ÓÉÏ
\0*\0
µÄǰ׺¡£ÕâÀïµÄ \0 ±íʾ ASCII ÂëΪ 0 µÄ×Ö·û£¬¶ø²»ÊÇ \0 ×éºÏ¡£
private ÉùÃ÷µÄ×Ö¶ÎΪ˽ÓÐ×ֶΣ¬Ö»ÔÚËùÉùÃ÷µÄÀàÖпɼû£¬ÔÚ¸ÃÀàµÄ×ÓÀàºÍ¸ÃÀàµÄ¶ÔÏóʵÀýÖоù²»¿É¼û¡£Òò´Ë˽ÓÐ×ֶεÄ×Ö¶ÎÃûÔÚÐòÁл¯Ê±£¬×Ö¶ÎÃûÇ°Ãæ»á¼ÓÉÏ
\0<declared class name>\0µÄǰ׺¡£ÕâÀï <declared class name> ±íʾµÄÊÇÉùÃ÷¸Ã˽ÓÐ×ֶεÄÀàµÄÀàÃû£¬¶ø²»ÊDZ»ÐòÁл¯µÄ¶ÔÏóµÄÀàÃû¡£ÒòΪÉùÃ÷¸Ã˽ÓÐ×ֶεÄÀ಻һ¶¨ÊDZ»ÐòÁл¯µÄ¶ÔÏóµÄÀ࣬¶øÓпÉÄÜÊÇËüµÄ×æÏÈÀà¡£
×Ö¶ÎÃû±»×÷Ϊ×Ö·û´®ÐòÁл¯Ê±£¬×Ö·û´®ÖµÖаüÀ¨¸ù¾ÝÆä¿É¼ûÐÔËù¼ÓµÄǰ׺¡£×Ö·û´®³¤¶ÈÒ²°üÀ¨Ëù¼Óǰ׺µÄ³¤¶È¡£ÆäÖÐ \0 ×Ö·ûÒ²ÊǼÆË㳤¶ÈµÄ¡£
1×¢£º ÔÚ PHP ÊÖ²áÖУ¬×ֶα»³ÆΪÊôÐÔ£¬¶øʵ¼ÊÉÏ£¬ÔÚ PHP 5 ÖÐÒýÈëµÄÓà __set¡¢__get À´¶¨ÒåµÄ¶ÔÏó³ÉÔ±¸üÊʺϽÐ×öÊôÐÔ¡£ÒòΪÓà __set¡¢__get À´¶¨ÒåµÄ¶ÔÏó³ÉÔ±ÓëÆäËüÓïÑÔÖеÄÊôÐÔµÄÐÐΪÊÇÒ»Ö£¬¶ø PHP ÊÖ²áÖÐËù˵µÄÊôÐÔʵ¼ÊÉÏÔÚÆäËûÓïÑÔÖУ¨ÀýÈ磺C#£©Öб»³ÆΪ×ֶΣ¬ÎªÁ˱ÜÃâ»ìÏý£¬ÕâÀïÒ²³ÆΪ×ֶΣ¬¶ø²»ÊÇÊôÐÔ¡£
5£®Ç¶Ì׸´ºÏÀàÐ͵ÄÐòÁл¯
ÉÏÒ»ÕÂÌÖÂÛÁ˼òµ¥µÄ¸´ºÏÀàÐ͵ÄÐòÁл¯£¬´ó¼Ò»á·¢ÏÖ¶ÔÓÚ¼òµ¥µÄÊý×éºÍ¶ÔÏóÆäʵҲºÜÈÝÒס£µ«ÊÇÈç¹ûÓöµ½×Ô¼º°üº¬×Ô¼º»òÕß A °üº¬ B£¬B ÓÖ°üº¬ A ÕâÀàµÄ¶ÔÏó»òÊý×éʱ£¬PHP ÓÖ¸ÃÈçºÎÐòÁл¯ÕâÖÖ¶ÔÏóºÍÊý×éÄØ£¿±¾ÕÂÎÒÃǾÍÀ´ÌÖÂÛÕâÖÖÇé¿öϵÄÐòÁл¯ÐÎʽ¡£
5.1£®¶ÔÏóÒýÓúÍÖ¸ÕëÒýÓÃ
ÔÚ PHP ÖУ¬±êÁ¿ÀàÐÍÊý¾ÝÊÇÖµ´«µÝµÄ£¬¶ø¸´ºÏÀàÐÍÊý¾Ý£¨¶ÔÏóºÍÊý×飩ÊÇÒýÓô«µÝµÄ¡£µ«ÊǸ´ºÏÀàÐÍÊý¾ÝµÄÒýÓô«µÝºÍÓà & ·ûºÅÃ÷È·Ö¸¶¨µÄÒýÓô«µÝÊÇÓÐÇø±ðµÄ£¬Ç°ÕßµÄÒýÓô«µÝÊǶÔÏóÒýÓ㬶øºóÕßÊÇÖ¸ÕëÒýÓá£
ÔÚ½âÊͶÔÏóÒýÓúÍÖ¸ÕëÒýÓÃ֮ǰ£¬ÏÈÈÃÎÒÃÇ¿´¼¸¸öÀý×Ó¡£
<?php echo "<pre>"; class SampleClass { var $value; } $a = new SampleClass(); $a->value = $a; $b = new SampleClass(); $b->value = &$b; echo serialize($a); echo "\n"; echo serialize($b); echo "\n"; echo "</pre>"; ?>Õâ¸öÀý×ÓµÄÊä³ö½á¹ûÊÇÕâÑùµÄ£º
O:11:"SampleClass":1:{s:5:"value";r:1;} O:11:"SampleClass":1:{s:5:"value";R:1;}´ó¼Ò»á·¢ÏÖ£¬ÕâÀï±äÁ¿ $a µÄ value ×ֶεÄÖµ±»ÐòÁл¯³ÉÁË r:1£¬¶ø $b µÄ value ×ֶεÄÖµ±»ÐòÁл¯³ÉÁË R:1¡£
µ«ÊǶÔÏóÒýÓúÍÖ¸ÕëÒýÓõ½µ×ÓÐʲôÇø±ðÄØ£¿
´ó¼Ò¿ÉÒÔ¿´ÏÂÃæÕâ¸öÀý×Ó£º
echo "<pre>"; class SampleClass { var $value; } $a = new SampleClass(); $a->value = $a; $b = new SampleClass(); $b->value = &$b; $a->value = 1; $b->value = 1; var_dump($a); var_dump($b); echo "</pre>";´ó¼Ò»á·¢ÏÖ£¬ÔËÐнá¹ûÒ²Ðí³öºõÄãµÄÔ¤ÁÏ£º
object(SampleClass)#1 (1) { ["value"]=> int(1) } int(1)¸Ä±ä $a->value µÄÖµ½ö½öÊǸıäÁË $a->value µÄÖµ£¬¶ø¸Ä±ä $b->value µÄֵȴ¸Ä±äÁË $b ±¾Éí£¬Õâ¾ÍÊǶÔÏóÒýÓúÍÖ¸ÕëÒýÓõÄÇø±ð¡£
²»¹ýºÜ²»ÐÒµÄÊÇ£¬PHP ¶ÔÊý×éµÄÐòÁл¯·¸ÁËÒ»¸ö´íÎó£¬ËäÈ»Êý×é±¾ÉíÔÚ´«µÝʱҲÊǶÔÏóÒýÓô«µÝ£¬µ«ÊÇÔÚÐòÁл¯Ê±£¬PHP ËƺõÍü¼ÇÁËÕâÒ»µã£¬¿´ÏÂÃæµÄÀý×Ó£º
echo "<pre>"; $a = array(); $a[1] = 1; $a["value"] = $a; echo $a["value"]["value"][1]; echo "\n"; $a = unserialize(serialize($a)); echo $a["value"]["value"][1]; echo "</pre>";½á¹ûÊÇ£º1
´ó¼Ò»á·¢ÏÖ£¬½«ÔÊý×éÐòÁл¯ÔÙ·´ÐòÁл¯ºó£¬Êý×é½á¹¹±äÁË¡£Ô±¾ $a["value"]["value"][1] ÖеÄÖµ 1£¬ÔÚ·´ÐòÁл¯Ö®ºó¶ªÊ§ÁË¡£
ÔÒòÊÇʲôÄØ£¿ÈÃÎÒÃÇÊä³öÐòÁл¯Ö®ºóµÄ½á¹ûÀ´¿´Ò»¿´£º
$a = array(); $a[1] = 1; $a["value"] = $a; echo serialize($a);½á¹ûÊÇ£º
a:2:{i:1;i:1;s:5:"value";a:2:{i:1;i:1;s:5:"value";N;}}
ÔÀ´£¬ÐòÁл¯Ö®ºó£¬$a["value"]["value"] ±ä³ÉÁË NULL£¬¶ø²»ÊÇÒ»¸ö¶ÔÏóÒýÓá£
Ò²¾ÍÊÇ˵£¬PHP Ö»¶Ô¶ÔÏóÔÚÐòÁл¯Ê±²Å»áÉú³É¶ÔÏóÒýÓñêʾ£¨r£©¡£¶ÔËùÓеıêÁ¿ÀàÐͺÍÊý×飨Ҳ°üÀ¨ NULL£©ÐòÁл¯Ê±¶¼²»»áÉú³É¶ÔÏóÒýÓᣵ«ÊÇÈç¹ûÃ÷ȷʹÓÃÁË & ·ûºÅ×÷µÄÒýÓã¬ÔÚÐòÁл¯Ê±£¬»á±»ÐòÁл¯ÎªÖ¸ÕëÒýÓñêʾ£¨R£©¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ