对象序列化/反序列化如何工作? [英] How does object serialize/unserialize work?

查看:124
本文介绍了对象序列化/反序列化如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关PHP的序列化/反序列化概念的信息.我想知道它们如何存储在filesystem/db中.我猜它是二进制格式.但是,我想知道整个类是如何存储的?我了解可以存储数据成员中的数据,但是方法如何存储?

I was reading about serialize/unserialize concepts of PHP. I was wondering how they are stored in the filesystem/db. I guess it is in the binary format. However, I wonder how entire class is stored? I understood that data in the data member can be stored but how are the methods stored?

我的意思是,PHP如何知道在someFunc()函数内部编写了什么代码?

I mean, how does PHP know what code is written inside the function of say, someFunc()?

$obj = new ClassName();
$obj->someFunc();
$serial = serialize($obj);
$unserialobj = unserialize($serial);
$unserialobj->someFunc();

PHP可以知道在第2行做什么,但是如何知道在第5行这是一个非序列化对象呢?它也会保存代码吗?

PHP can know what to do at line #2, but how it know what to do at line #5 which is an unserialized object? Does it save the code as well?

推荐答案

PHP可以知道在第二行执行的操作,但是如何知道在第五行执行的操作(这是未序列化的对象)呢?它也会保存代码吗?

PHP can know what to do at line#2 but how it knows what to do at line#5 which is a unserialized object? does it save the code as well?

是的,serialize()将保存有关该对象是其实例的类的信息以及其状态,因此,在反序列化时,您将获得该类的实例,在本例中为ClassName.

Yes, serialize() will save the information about the class which this object is an instance of, along with its state, so when you unserialize, you get an instance of that class, which in this case is ClassName.

这篇关于对象序列化/反序列化如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆