Advanced OOP technology for br> PHP
After looking at basic OOP concepts, I can show you more advanced technologies:
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, which means holding
There is 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 hold
There is 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 and 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
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 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.