zoukankan      html  css  js  c++  java
  • php函数serialize()与unserialize()

    serialize()和unserialize()在php手册上的解释是:

    serialize — Generates a storable representation of a value

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

    unserialize — Creates a PHP value from a stored representation

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

    <?php
    //声明一个类
    class dog
    {

    var $name;
    var $age;
    var $owner;

    function dog($in_name = "unnamed", $in_age = "0", $in_owner = "unknown")
    {
    $this->name = $in_name;
    $this->age = $in_age;
    $this->owner = $in_owner;
    }

    function getage()
    {
    return ($this->age * 365);
    }

    function getowner()
    {
    return ($this->owner);
    }

    function getname()
    {
    return ($this->name);
    }
    }
    //实例化这个类
    $ourfirstdog = new dog("Rover", 12, "Lisa and Graham");
    //用serialize函数将这个实例转化为一个序列化的字符串
    $dogdisc = serialize($ourfirstdog);
    print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:"dog":3:{s:4:"name";s:5:"Rover";s:3:"age";i:12;s:5:"owner";s:15:"Lisa and Graham";}


    print '<BR>';

    /*
    -----------------------------------------------------------------------------------------
    在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件
    -----------------------------------------------------------------------------------------
    */

    //我们在此注销这个类
    unset($ourfirstdog);

    /* 还原操作 */

    /*
    -----------------------------------------------------------------------------------------
    在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件
    -----------------------------------------------------------------------------------------
    */

    //我们在这里用 unserialize() 还原已经序列化的对象
    $pet = unserialize($dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了
    //获得年龄和名字属性

    $old = $pet->getage();
    $name = $pet->getname();
    //这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态
    print "Our first dog is called $name and is $old days old<br>";
    print '<BR>';
    ?>


    转:http://www.cnblogs.com/yeer/archive/2009/03/25/1421161.html

  • 相关阅读:
    curl 的使用,参数大全
    ubuntu 编译安装php5扩展
    ubuntu 编译安装 php5.6.40
    ubuntu 编译安装 mysql5.7.32
    ubuntu 编译安装 apache2.4.46
    Android 逆向工具总结
    mariadb 和 mysql 之间的版本关系
    ubuntu 使用 apt 安装 apache2 php7 mysql8
    linux 系统安装 pip
    P2756 飞行员配对方案问题 提交 13.91k 通过 7.59k 时间限制 1.00s 内存限制 125.00MB 返回题目
  • 原文地址:https://www.cnblogs.com/shuaixf/p/2409944.html
Copyright © 2011-2022 走看看