PHP Advanced OOP Technology Demo

Source: Internet
Author: User
Tags arrays constructor current time explode html form php and serialization

If you understand basic OOP concepts, I will show you more advanced techniques.

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);
  //保存$str到磁盘上
//几个月以后
//从磁盘中装入str
 
  $obj2=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.

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 call the class whenever you want to use it. If you have an HTML form, the user can select a product by selecting the Product ID number. In the database has the product information, you want to display the product, displays 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 a database. You can use OOP or PHP to reduce coding and improve quality.

Define a product's class, define the methods it should have (for example: Display), and then define the classes for each type of product, from the Product class Pie (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, etc. fields) based on the Type field named for each product in the database. Then in the processing script, you can take the type value out of the database and instantiate an object named type:

<?php
$obj=new $type();
$obj->action();

This is a very good feature of PHP, you can call the $obj display method or other method without considering the type of object. 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 the objects, implement them in different ways in different classes, and then use them in the main script for any object, no if...else, no two programmers, just be happy.

Now you agree that programming is easy, maintenance is cheap, reusable is true?

If you manage a group of programmers, assigning work is simple, and each person may be responsible for one type of object and the class that handles it.

You can internationalize through this technology, apply the appropriate classes to the language fields that the user chooses, and so on.

Copy and Clone

When you create a $obj object, you can copy the object by $obj2= $obj, and the new object is a copy of $obj (not a reference), so it has $obj state at the time. Sometimes you don't want to, you just want to generate a new object like the Obj class, you can invoke the constructor of the class by using the new statement. It can also be done in PHP by serialization, and a base class, but all the other classes derive from the base class.

Enter the danger zone.

When you serialize an object, you get a string of some form, and if you're interested, you can tune it, where the string has the name of the class (great!). ), you can take it out like this:

<?php
$herring=serialize($obj);
$vec=explode(':',$herring);
$nam=str_replace("\"",'',$vec[2]);

So suppose you create a "universe" class and force all classes to extend from universe, you can define a clone in universe, as follows:

<?php
function clone() {
$herring=serialize($this);
$vec=explode(':',$herring);
$nam=str_replace("\"",'',$vec[2]);
$ret=new $nam;
return $ret;
}
}
//然后
$obj=new Something();
//从Universe扩展
$other=$obj->clone();

What you get is a new something class object that uses the same object that the constructor creates by using the new method. I don't know if this works for you, but the Universe class can know the name of a derived class is a good experience. Imagination is the only limit.

This statement is written to the current time.

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.