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

Source: Internet
Author: User
Tags arrays variables serialization
Programming | object | Project PHP object-oriented programming: Ways to develop large PHP projects (IV.)





Author: Luis Argerich translator: Limodou





PHP's Advanced OOP technology


after looking at basic OOP concepts, I can show you more advanced technology:





serialization (serializing)


PHP does not support persistent objects, and in OOP permanent objects are objects that can maintain state and functionality in multiple application references, meaning that the


has the ability to save objects to a file or database, and you can load objects at a later time. This is called the serialization mechanism. PHP


has a serialization method that can be invoked through an object, and the serialization method can return the string representation of an object. However, serialization only preserves the


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, then deserialize the object to $obj, you can continue


How to use objects! 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 can lead to a misunderstanding when you save a serialized version to disk and exit the script. When you run this script later, you


cannot expect the object's method to be there when an object is deserialized, because the string representation does not include the method at all.





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


in 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 the
only through a similar use of $obj2->x

member variables (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.








Transferred from Phpbuilder.com














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.