欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP串行化與反串行化實(shí)例分析

 更新時(shí)間:2016年12月27日 08:58:20   作者:EthanQ  
這篇文章主要介紹了PHP串行化與反串行化,結(jié)合實(shí)例形式分析了php面向?qū)ο蟪绦蛟O(shè)計(jì)及serialize與unserialize函數(shù)進(jìn)行串行化、反串行化相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP串行化與反串行化。分享給大家供大家參考,具體如下:

對(duì)象也是一種在內(nèi)存中存儲(chǔ)的數(shù)據(jù)類型,他的壽命通常隨著生成該對(duì)象的程序的終止而終止。有時(shí)候可能需要把對(duì)象的狀態(tài)保存下來,需要時(shí)再將其回復(fù)。串行化是把每個(gè)對(duì)象轉(zhuǎn)化為二進(jìn)制字符串。

<?php
class Person {
  var $name;
  var $sex;
  var $age;
  function __construct($name = "", $sex = "男", $age = 22) {
    $this->name = $name;
    $this->sex = $sex;
    $this->age = $age;
  }
  function say() {
    echo $this->name . "在說話<br/>";
  }
  function run() {
    echo "在走路·<br/>";
  }
  //串行化的時(shí)候自動(dòng)調(diào)用,成員$sex被忽略,只串行$name,$age
  function __sleep() {
    $arr = array("name","age");
    return $arr;
  }
  //反串行化時(shí)自動(dòng)調(diào)用
  function __wakeup() {
    $this->age = 33;
  }
}
class Student extends Person {
  var $school;
  function __construct($name = "", $sex = "男", $age = 22,$school="") {
    parent::__construct($name,$sex,$age);
    $this->school = $school;
  }
  function study() {
    echo $this->name."正在".$this->school."學(xué)習(xí)<br/>";
  }
}
class Teacher extends Student {
  var $wage;
  function teaching() {
    echo $this->name."正在".$this->school."教學(xué),每月工資為".$this->wage."<br/>";
  }
  //如果調(diào)用了不存在的方法,將會(huì)自動(dòng)調(diào)用__call(),不會(huì)報(bào)錯(cuò)
  function __call($functionName,$args) {
    echo "函數(shù)名:".$functionName;
    print_r($args);
    echo "<br/>";
  }
}
$teacher1 = new Teacher("kaifu","男",22);
$teacher1->school = "edu";
$teacher1->wage = 4000;
$teacher1->say();
$teacher1->study();
$teacher1->teaching();
$teacher1->hello(1,2,3);
?>

<?php
  require_once 'Person.php';
  $teacher = new Teacher("tom","男",22);
  $teacher_str = serialize($teacher);
  file_put_contents("file.txt", $teacher_str);
  //反串行化
  $objStr = file_get_contents("file.txt");
  $t = unserialize($objStr);
  echo $t->age;
?>

串行化 file.txt :

O:7:"Teacher":2:{s:4:"name";s:3:"tom";s:3:"age";i:22;}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論