Home > Backend Development > PHP Problem > What are the php magic methods?

What are the php magic methods?

coldplay.xixi
Release: 2023-03-08 19:30:01
Original
3470 people have browsed it

php magic method: 1. [_sleep()] controls the actual processing part when the object is serialized; 2. [_wakeup()] restores the object attributes after deserialization; 3. [_toString()] Mechanism for converting objects into strings.

What are the php magic methods?

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer.

php magic method:

_sleep() can control the part that is actually processed when the object is serialized

_wakeup() restores after deserialization Object attributes

_toString() Mechanism for converting objects into strings

Convert PHP variables into a string of encoded strings, the method is serialize() Deserialize unserialize()

  //序列化
  class testSerialize{
   public $a = 10;
   public $b = 15;
   public $c = 20;
   function _construct(){
   $this->b = $this->a * 10;
   $this->c = $this->b * 2;
   } 
  }
$k = serialize(new testSerialize());
echo $k;//
out:  O:13:"testSerialize":3:{s:1:"a";i:10;s:1:"b";i:15;s:1:"c";i:20;}
$j = unserialize($k);
Copy after login

sleep method:

class testSerialize1{
   public $a = 10;
   public $b = 15;
   public $c = 20;
   function _construct(){
   $this->b = $this->a * 10;
   $this->c = $this->b * 2;
   } 
   function __sleep(){
   return $this->a;
   }
  }
$k = serialize(new testSerialize1());
echo $k;
Copy after login

The same applies to other methods

Related video recommendations: PHP programming from entry to proficiency

The above is the detailed content of What are the php magic methods?. 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