PHP Advanced OOP Technology Demo _php Tutorial

Source: Internet
Author: User
Serialization (serializing)
While PHP does not support persistent objects, permanent objects in OOP are objects that can hold state and functionality in references to multiple apps, which means having the ability to save objects to a file or database, and to load objects later. This is called the serialization mechanism. PHP has a serialization method that can be called through an object, and the serialization method can return a string representation of an object. However, serialization saves only the member data of the object and does not package the session method.
In PHP4, if you serialize the object into a string $s, then dispose of the object, and then deserialize the object
To $obj, you can continue to use the object Method! I do not recommend this because (a) there is no guarantee in the documentation that this behavior will still be available in later versions. (b) This may lead to a misunderstanding when you save a serialized version to disk and exit the script. When you run the script later, you cannot expect the object's methods to be there when deserializing an object, because the string representation does not include the method at all.
In summary, PHP serialization is useful for saving member variables of an object. (You can also serialize related arrays and arrays into a file).
Example:
Copy CodeThe code is as follows:
<?php
$obj =new Classfoo ();
$str =serialize ($obj);
Save $str to disk
A few months later
Mount Str from disk
 
$obj 2=unserialize ($STR)

You restored the member data, but did not include the method (according to the document). This leads to access to member variables only by similar use of $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 delighted to welcome the full serialization feature in subsequent versions of PHP.
Using classes for data storage PHP and oop a very good thing is that you can easily define a class to manipulate something, and you can invoke the appropriate class whenever you want to use it. Suppose you have an HTML form that allows users to select a product by selecting the Product ID number. In the database has the product information, you want to display the product, show its price and so on. You have different types of products, and the same action may have different meanings for different products. For example, displaying a sound might mean playing it, but for other kinds of products it might mean displaying a picture in the database. You can use OOP or PHP to reduce coding and improve quality.
Define a product's class, define the method it should have (for example: Display), and then define the product for each type
Class, come out of the product class (Sounditem class, Viewableitem class, etc.) and cover the methods in the product class so that they act as you think.
A typical product table may have (ID, type, price, description, and so on), depending on the type of each product in the database, given the class name. Then in the processing script, you can remove the type value from the database and instantiate an object named type:
Copy CodeThe code is as follows:
<?php
$obj =new $type ();
$obj->action ();

This is a very good feature of PHP, you can not consider the type of object, call $obj display method or other methods. With this technique, you don't need to modify the script to add a new type of object, just add a class that handles it.
This is a powerful feature, as long as you define the method without considering the types of all objects, implement them differently in different classes, and then use them in the main script for any object, no if...else, no two programmers,
Only Happy.
Now do you agree that programming is easy, maintenance is cheap, reusable is true?
If you manage a group of programmers, assigning jobs is easy, and everyone may be responsible for one type of object and
The class that handles it.
Internationalization can be achieved through this technology, according to the user's chosen language field to apply the corresponding class can be, and so on.
Copy and Clone
When you create a $obj object, you can copy the object through the $obj2= $obj, and the new object is a copy of the $obj (not a reference), so it has a $obj state at that time. Sometimes, you don't want to, you just want to create a new object like the Obj class, you can call the class's constructor by using the new statement. It can also be implemented in PHP by serialization, and a base class, but all other classes are derived from the base class.
Enter hazardous areas
When you serialize an object, you get a string of some format, and if you're interested, you can tune it, where the string has the name of the class (too good!). ), you can take it out, like this:
Copy CodeThe code is as follows:
<?php
$herring =serialize ($obj);
$vec =explode (': ', $herring);
$nam =str_replace ("\" "," ', $vec [2]);

So suppose you create a "Universe" class and force all classes to be extended from Universe, you can define a clone method in Universe, as follows:
Copy CodeThe code is as follows:
<?php
function Clone () {
$herring =serialize ($this);
$vec =explode (': ', $herring);
$nam =str_replace ("\" "," ', $vec [2]);
$ret =new $nam;
return $ret;
}
}
And then
$obj =new Something ();
Extending from Universe
$other = $obj->clone ();

What you get is a new object of the Something class, which is the same as using the new method, which calls the constructor to create the object. I don't know if this is useful for you, but the Universe class can know the name of the derived class as a good experience. Imagination is the only limit.
This statement is written to the current time.

http://www.bkjia.com/PHPjc/320543.html www.bkjia.com true http://www.bkjia.com/PHPjc/320543.html techarticle serialization (serializing) PHP does not support persistent objects, and in OOP persistent objects are objects that can hold state and functionality in a reference to multiple apps, which means having an object saved to a text ...

  • 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.