Blogger Information
Blog 57
fans 0
comment 0
visits 46975
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对象的序列化
藍錄的博客
Original
883 people have browsed it

实例

<?php
/**
 * 对象的序列化
 */
//$num = 500;
//echo serialize($num),'<hr>'; // i:500;
//$name = 'peter';
//echo serialize($name), '<hr>'; // s:5:"peter";
//$course = ['html','css','javascript'];
//echo serialize($course), '<hr>';

class Db
{
    //连接参数与返回值
    public $db = null;
    public $host;
    public $user;
    public $pass;

    //构造方法
    public function __construct($host='127.0.0.1', $user='root', $pass='root')
    {
        $this->host = $host;
        $this->user =  $user;
        $this->pass = $pass;
        // 确保实例化对象的时候能自动连接上数据库
        $this->connect();
    }

    //数据库连接
    private function connect()
    {
        $this->db=mysqli_connect($this->host,$this->user,$this->pass);
    }

    //对象序列化的时候自动调用
    public function __sleep()
    {
        return ['host','user','pass'];
    }

    //反序列化:
    public function __wakeup()
    {
        $this->connect();
    }

}

$obj = new Db();
/**
 * 对象序列化的特点:
 * 1. 只保存对象中的属性,不保存方法
 * 2. 只保存类名,不保存对象名
 */

echo serialize($obj),'<hr>';
$tmp = serialize($obj);
var_dump(unserialize($tmp));

运行实例 »

点击 "运行实例" 按钮查看在线实例

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post