PHP advanced OOP technical demo _ PHP Tutorial

Source: Internet
Author: User
PHP advanced OOP technology demonstration. Serializing PHP does not support permanent objects. in OOP, permanent objects are objects that can maintain state and function in multiple application references, this means that you can save the object to a serialized object)
PHP does not support permanent objects. in OOP, permanent objects are objects that can maintain the state and function in the references of multiple applications, this means that the object can be saved to a file or database, and objects can be loaded later. This is the so-called serialization mechanism. PHP has a serialization method, which can be called through an object. the serialization method can return the string representation of an object. However, serialization only saves the member data of the object without the package method.
In PHP4, if you serialize an object to the string $ s, release the object, and then deserialize the object
To $ obj, you can continue to use the object method! I do not recommend that you do this because (a) files do not guarantee that this behavior can still be used in future versions. (B) this may lead to a misunderstanding when you save a serialized version to the disk and exit the script. When you run this script in the future, you cannot expect that when deserializing an object, the object method will also be there, because the string representation does not include the method at all.
All in all, PHP Serialization is very useful for saving object member variables. (You can also serialize related arrays and arrays to a file ).
Example:

The code is as follows:


<? Php
$ Obj = new Classfoo ();
$ Str = serialize ($ obj );
// Save $ str to disk
// A few months later
// Input str from the disk
 
$ Obj2 = unserialize ($ str)


You have recovered the member data, but do not include methods (as described in this document ). As a result, you can only use $ obj2-> x to access member variables (you have no other way !) So do not 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 very pleased to welcome the full serialization feature in subsequent PHP versions.
A very good thing about using classes for data storage PHP and OOP is that you can easily define a class to operate on something, you can call the corresponding classes whenever you want to use them. Suppose you have an HTML form. you can select a product by selecting the product ID. There is product information in the database. you want to display the product and its price. You have different types of products, and the same action may have different meanings for different products. For example, displaying a sound may mean playing it, but for other products, it may mean displaying an image in the database. You can use OOP or PHP to reduce coding and improve quality.
Define a product class, define its expected method (for example, display), and then define each type of product
From the product class (SoundItem class, ViewableItem class, etc.), override the methods in the product class, so that they can act as you want.
Name the class according to the type field of each product in the database. a typical product table may have (id, type, price, description, and other fields )... in the processing script, you can retrieve the type value from the database and instantiate an object named type:

The code is as follows:


<? Php
$ Obj = new $ type ();
$ Obj-> action ();


This is a very good feature of PHP. you can call the $ obj display method or other methods without considering the object type. With this technology, you do not need to modify the script to add a new type of object, but add a class to process it.
This function is very powerful, as long as you define methods, instead of considering the types of all objects, implement them in different classes according to different methods, and then use them for any objects in the main script, no if... else does not need two programmers either,
Only happy.
Now you agree that programming is easy, maintenance is cheap, and reusable is true?
If you manage a group of programmers, it is easy to assign jobs. each person may be responsible for a type of object and
Class that processes it.
You can use this technology to achieve internationalization and apply the corresponding classes based on the language fields selected by the user.
Copy and clone
When you create a $ obj object, you can use $ obj2 = $ obj to copy the object. The new object is a copy of $ obj (not a reference ), therefore, it has the status of $ obj at that time. Sometimes, you just want to generate a new object like the obj class. you can call the class constructor by using the new statement. PHP can also be implemented through serialization and a base class, but all other classes must be derived from the base class.
Enter Dangerous area
When you serialize an object, you will get a string of a certain format. if you are interested, you can call it. among them, there is a class name in the string (great !), You can take it out, such:

The code is as follows:


<? Php
$ Herring = serialize ($ obj );
$ Vec = explode (':', $ herring );
$ Nam = str_replace ("\" ",'', $ vec [2]);


So suppose you have created a "Universe" class and forced all classes to be extended from universe. you can define a clone method in universe, as shown below:

The code is as follows:


<? Php
Function clone (){
$ Herring = serialize ($ this );
$ Vec = explode (':', $ herring );
$ Nam = str_replace ("\" ",'', $ vec [2]);
$ Ret = new $ nam;
Return $ ret;
}
}
// Then
$ Obj = new Something ();
// Extend from Universe
$ Other = $ obj-> clone ();


What you get is a new Something Class object, which is the same as the object created by calling the constructor using the new method. I don't know if this is useful to you, but the Universe class can know that the name of a derived class is a good experience. Imagination is the only restriction.
This statement is the current write time.

Serialize (Serializing) PHP does not support permanent objects. in OOP, permanent objects are objects that can maintain state and function in multiple application references, this means that you have to save the object to a file...

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.