PHP serialization and deserialization example analysis

墨辰丷
Release: 2023-03-28 10:08:02
Original
1465 people have browsed it

This article mainly introduces PHP serialization and deserialization, and analyzes PHP object-oriented programming and the serialization and deserialization related usage skills of the serialize and unserialize functions in the form of examples. Friends who need it You can refer to the following examples of

This article describes PHP serialization and deserialization. Share it with everyone for your reference, the details are as follows:

Object is also a data type stored in memory, and its lifespan usually ends with the termination of the program that generated the object. Sometimes you may need to save the state of an object and restore it when needed. Serialization converts each object into a binary string.

<?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/>";
  }
  //串行化的时候自动调用,成员$sex被忽略,只串行$name,$age
  function __sleep() {
    $arr = array("name","age");
    return $arr;
  }
  //反串行化时自动调用
  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."学习<br/>";
  }
}
class Teacher extends Student {
  var $wage;
  function teaching() {
    echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."<br/>";
  }
  //如果调用了不存在的方法,将会自动调用__call(),不会报错
  function __call($functionName,$args) {
    echo "函数名:".$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);
?>
Copy after login

<?php
  require_once &#39;Person.php&#39;;
  $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;
?>
Copy after login

Serialized file.txt:

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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP Object-Oriented-ObjectSerializationserialize(),__sleep(),__wakeup Detailed explanation of the code of ()

Serialization usage example in PHP

PHP The use of serial

The above is the detailed content of PHP serialization and deserialization example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!