Method for developing large PHP projects (4)

xiaoxiao2021-03-06  48

Author: Luis Argerich Translator: limodou

Data storage using classes

For PHP and OOP a very good thing is that you can easily define a class to operate something, and whenever you want to use it, you can call the appropriate classes. Suppose you have an HTML form, users can choose a product by selecting the product ID number. In the database, there is information, you want to display the product, showing its price, and so on. You have different types of products, and the same action may have a different meaning of different products. For example, displaying a sound may mean playing it, but for other kinds of products may mean displaying a picture in the database. You can use OOP or PHP to reduce encoding and improve quality:

Define a class of a product, define what it should have (for example, display), then define the class of each type of product, from the product class, SoundItem class, viewableItem class, etc.), overwritten in the product The method in the class makes them actions in your thoughts.

Depending on the type (Type) field of each product in the database, a typical product table may have (ID, Type, Price, Description, etc. "and then in the process of handling the script, you can from the database Take the TYPE value and instantiate a object called Type:

--------------------------------

$ OBJ = New $ TYPE ();

$ obj-> action ();

?>

This is a very good feature of PHP, you can call a display method or other method of the $ OBJ without considering the type of object. With this technology, you don't need to modify the script to add a new type of object, just add a class that handles it.

This feature is very powerful, as long as the method is defined, do not consider the type of all objects, implement them in different classes, and then use them to any object in the main script, no if ... Else, nor Two programmers are needed, only happy.

Now you agree to programming, maintenance is cheap, can you reuse it?

If you manage a group of programmers, allocation work is very simple, each person may be responsible for a type of object and processing it.

It can be achieved by this technology, and the corresponding class is applied according to the language field selected by the user, and so on.

Copy and clone

When you create an object of $ OBJ, you can copy objects through $ OBJ2 = $ OBJ, the new object is a copy of $ OBJ (not a reference), so it has $ OBJ at the time. Sometimes, you don't want this, you just want to generate a new object like an OBJ class, you can call the class constructor by using the New statement. In PHP can also be implemented by serialization, and a base class, but all other classes are derived from the base class.

Enter the dangerous area

When you serialize an object, you will get a string of a format. If you are interested, you can escape it, where there is a name in the string (too good!), You can take it out , Icon:

----------------------------------------

$ HERRING = Serialize ($ OBJ);

$ VC = EXPLODE (':', $ HERRING);

$ nam = str_replace ("", '', $ vec [2]);?>

So, assume that you create a "universe" class and force all classes must extend from the universe, you can define a Clone method in the universe, as follows:

-----------------------------------

Class universe {

Function clone () {

$ HERRING = Serialize ($ this);

$ VC = EXPLODE (':', $ HERRING);

$ nam = str_replace ("" ", '', $ vec [2]);

$ RET = New $ NAM;

Return $ RET;

}

}

//then

$ obj = new thing ";

// Expand from universe

$ other = $ obj-> clone ();

?>

What you get is an object of a new Something class, which uses the New method to call the object created by the constructor. I don't know if this is useful to you, but the universe class can know the name of the derived class is a good experience. Imagination is the only limit.

Note: I use PHP4, some things I have written may not work in PHP3.

转载请注明原文地址:https://www.9cbs.com/read-112375.html

New Post(0)