object-oriented Programming in PHP: Ways to develop large PHP projects (IV.)

Source: Internet
Author: User
Tags arrays variables serialization
Advanced OOP techniques in PHP
After looking at basic OOP concepts, I can show you more advanced technologies:

Serialization (serializing)
PHP does not support persistent objects, where permanent objects are objects that can maintain state and functionality in multiple application references, which means that you have the ability to save objects to a file or database, and you can load objects later. This is called the serialization mechanism. PHP has a serialization method that can be invoked through objects, and the serialization method can return the string representation of an object. However, serialization saves only the member data of the object without wrapping the method.

In PHP4, if you serialize an object into a string $s, then release the object and then deserialize the object to $obj, you can continue using the object's Method! I do not recommend doing this because (a) there is no guarantee in the document that this behavior can still be used in future releases. (b) This may lead to a misunderstanding when you save a serialized version to disk and exit the script. When you run this script later, you can't expect the object's method to be there when you deserialize an object, because the string representation does not include the method at all.

In summary, the serialization of PHP is useful for saving the member variables of an object. (You can also serialize related arrays and arrays to a file).

Example:

--------------------------------------------------------------------------------
<?php

$obj =new Classfoo ();
$str =serialize ($obj);

Save $str to disk


A few months later


Mount Str from disk

$obj 2=unserialize ($STR)

?>--------------------------------------------------------------------------------
You have restored the member data, but not the method (according to the document). This leads to access to member variables only by using $obj2->x (you have no other way!). The only way, so don't try it at home.

There are some ways to solve this problem, I keep it, because they are too bad for this concise article.

I would be happy to welcome the full serialization feature in subsequent versions of PHP.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.