php中对象的串行化,php对象串行化
php中对象的串行化,php对象串行化
我们大家有知道PHP串行化可以把变量包括对象,转化成连续bytes数据,你可以将串行化后的变量存在一个文件里或在网络上传输,然后再反串行化还原为原来的数据。文章这里就PHP串行化为大家详细的介绍。你在反串行化类的对象之前定义的类,PHP可以成功地存储其对象的属性和方法. 有时你可能需要一个对象在反串行化后立即执行。为了这样的目的,PHP会自动寻找__sleep和__wakeup方法。
当一个对象被PHP串行化,PHP会调用__sleep方法(如果存在的话). 在反串行化一个对象后,PHP 会调用__wakeup方法. 这两个方法都不接受参数. __sleep方法必须返回一个数组,包含需要串行化的属性. PHP会抛弃其它属性的值。如果没有__sleep方法,PHP将保存所有属性。
<?<span>php </span><span>/*</span><span> * * @Authors peng--jun * @Email 1098325951@qq.com * @Date 2016-01-23 14:40:38 * @Link </span><span>http://www.cnblogs.com/xs-yqz/</span><span> * @version $Id$ ========================================== </span><span>*/</span><span> header(</span><span>"</span><span>Content-type: text/html; charset=UTF-8</span><span>"</span><span>); </span><span>class</span><span> Person{ </span><span>private</span><span> $name; </span><span>private</span><span> $sex; </span><span>private</span><span> $age; function __construct($name,$age,$sex){ $</span><span>this</span>->name =<span> $name; $</span><span>this</span>->age =<span> $age; $</span><span>this</span>->sex =<span> $sex; } function say(){ echo </span><span>"</span><span>我的名字:</span><span>"</span>.$<span>this</span>->name.<span>"</span><span>性别为: </span><span>"</span>.$<span>this</span>->sex.<span>"</span><span>年龄为:</span><span>"</span>.$<span>this</span>-><span>age; } <span>//在类中添加此方法,在串行化的时候自动调用并返回数组</span> function __sleep(){ $arr </span>= array(<span>"</span><span>name</span><span>"</span>,<span>"</span><span>age</span><span>"</span>);<span>//</span><span>数组中的成员$name和$age将被串行化,成员$sex则将被忽略。</span> <span>return</span><span>($arr);<span>//使用__sleep()方法的时候必须返回一个数组。</span> } <span> //在反串行化对象时自动调用该方法,没有参数也没有返回值</span> function __wakeup(){ $</span><span>this</span>->age = <span>40</span><span>;<span>//在重新组织对象的时候,为新对象中的$age属性重新赋值</span> } } $person1 </span>= <span>new</span> Person(<span>"</span><span>张三</span><span>"</span>,<span>20</span>,<span>"</span><span>男</span><span>"</span><span>); $person1_string </span>=<span> serialize($person1); echo $person1_string.</span><span>"</span><span><br /></span><span>"</span><span>; </span><span>//</span><span>反串行化对象,并自动调用了__wakeup()方法重新为独享中的age赋值。</span> $person2 =<span> unserialize($person1_string); $person2</span>-><span>say(); </span>?>
输出的结果为:
O:<span>6</span>:<span>"</span><span>Person</span><span>"</span>:<span>2</span>:{s:<span>12</span>:<span>"</span><span>Personname</span><span>"</span>;s:<span>6</span>:<span>"</span><span>张三</span><span>"</span>;s:<span>11</span>:<span>"</span><span>Personage</span><span>"</span>;i:<span>20</span><span>;} 我的名字:张三性别为: 年龄为:</span><span>40</span>
2.将串行化的字符串保存到文件中,从文件中读取字符串,反串性化实例。
header(<span>"</span><span>Content-type: text/html; charset=UTF-8</span><span>"</span><span>); </span><span>class</span><span> Person{ </span><span>private</span><span> $name; </span><span>private</span><span> $sex; </span><span>private</span><span> $age; function __construct($name,$age,$sex){ $</span><span>this</span>->name =<span> $name; $</span><span>this</span>->age =<span> $age; $</span><span>this</span>->sex =<span> $sex; } function say(){ echo </span><span>"</span><span>我的名字:</span><span>"</span>.$<span>this</span>->name.<span>"</span><span>性别为: </span><span>"</span>.$<span>this</span>->sex.<span>"</span><span>年龄为:</span><span>"</span>.$<span>this</span>-><span>age; } } $person1 </span>= <span>new</span> Person(<span>"</span><span>张三</span><span>"</span>,<span>21</span>,<span>"</span><span>男</span><span>"</span><span>); $person1_string </span>=<span> serialize($person1); file_put_contents(</span><span>"</span><span>file.txt</span><span>"</span>, $person1_string);<br /><br />
header(<span>"</span><span>Content-type: text/html; charset=UTF-8</span><span>"</span><span>); </span><span>class</span><span> Person{ </span><span>private</span><span> $name; </span><span>private</span><span> $sex; </span><span>private</span><span> $age; function __construct($name,$age,$sex){ $</span><span>this</span>->name =<span> $name; $</span><span>this</span>->age =<span> $age; $</span><span>this</span>->sex =<span> $sex; } function say(){ echo </span><span>"</span><span>我的名字:</span><span>"</span>.$<span>this</span>->name.<span>"</span><span> 性别为:</span><span>"</span>.$<span>this</span>->sex.<span>"</span><span> 年龄为:</span><span>"</span>.$<span>this</span>-><span>age; } } $person2_string </span>= file_get_contents(<span>"</span><span>file.txt</span><span>"</span><span>); $person2 </span>=<span> unserialize($person2_string);<span>//反串性化重新形成对象$person2.</span> $person2</span>-><span>say(); </span>?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

Configuring and running PHP on IIS requires the following steps: 1) Download and install PHP, 2) Configuring IIS and adding FastCGI module, 3) Create and set up an application pool, 4) Create a website and bind to an application pool. Through these steps, you can easily deploy PHP applications on your Windows server and improve application stability and efficiency by configuring scaling and optimizing performance.
