菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
120
0

php魔术方法有哪些

原创
05/13 14:22
阅读数 524002

php魔术方法:1、【_sleep()】控制对象序列化时真正处理的部分;2、【_wakeup()】在反序列化后还原对象属性;3、【_toString()】对象转换成为字符串的机制。

本教程操作环境:windows7系统、PHP5.6版,DELL G3电脑。

php魔术方法:

_sleep() 可以控制对象序列化时真正处理的部分

_wakeup() 在反序列化后还原对象属性

_toString() 对象转换成为字符串的机制

把php变量转换成一串编码后字符串,方法为serialize() 反序列化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);

sleep方法:

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;

其他方法同理

相关视频推荐:PHP编程从入门到精通

以上就是php魔术方法有哪些的详细内容,更多请关注其它相关文章!

发表评论

0/200
120 点赞
0 评论
收藏
为你推荐 换一批