zoukankan      html  css  js  c++  java
  • PHP中的serialize()和unserialize()

    serialize — 产生一个可存储的值的表示

    unserialize — 从已存储的表示中创建 PHP 的值

     1 <?php
     2 class Person{
     3     private $name;
     4     private $age;
     5     private $sex;
     6 
     7     public function __construct($name, $age, $sex){
     8         $this->name = $name;
     9         $this->age = $age;
    10         $this->sex = $sex;
    11     }
    12 
    13     public function getname(){
    14         return $this->name;
    15     }
    16     public function getage(){
    17         return $this->age;
    18     }
    19     public function getsex(){
    20         return $this->sex;
    21     }
    22 }
    23 
    24 $personOne = new Person('weixing', '21', 'M');
    25 $personBefore = serialize($personOne);   //产生一个序列化的字符串
    26 //O:6:"Person":3:{s:12:"Personname";s:7:"weixing";s:11:"Personage";s:2:"21";s:11:"Personsex";s:1:"M";}
    27 $personAfter = unserialize($personBefore);  //转换为PHP的值,相当于$personOne
    28 print_r($personOne);
    29 echo "<br/>";
    30 print_r($personBefore);
    31 echo "<br/>";
    32 print_r($personAfter);
    33 ?>
  • 相关阅读:
    正则表达式和re模块
    面向对象基础
    面向对象高级
    网络编程
    collectiontimeossysjson模块
    继承
    封装
    我在吃饭
    111
    关于羽毛球拍
  • 原文地址:https://www.cnblogs.com/hell0x/p/5269331.html
Copyright © 2011-2022 走看看