Method for developing large PHP projects (3)

xiaoxiao2021-03-06  48

Author: Luis Argerich Translator: limodou

Overload (different from the overlay) is not supported in PHP. In OOP, you can overload a method to implement two or heavy methods have the same name, but there are different quantities or types of parameters (this depends on language). PHP is a loose type language, so it does not work by the type overload, however the number of times the number of parameters is not played.

Sometimes heavy load constructors in OOP are very good, so you can create objects (passing different quantities) through different ways. Tips for implementing it in PHP is:

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

Class myclass {

Function myclass () {

$ name = "myclass" .func_num_args ();

$ THIS -> $ name ();

/ Note $ this-> name () is generally wrong, but here $ NAME is a name that will be called method

}

Function myclass1 ($ x) {

CODE;

}

Function myclass2 ($ x, $ y) {

CODE;

}

}

?>

By processing in the class, using this class is transparent to the user:

$ obj1 = new myclass ('1'); // will call myclass1

$ obj2 = new myclass ('1', '2'); // will call myclass2

Sometimes this is very easy.

Polymorphism

Polymorphism is an object of the object, which can determine which object of the call according to the passable object parameters at runtime. For example, if you have a class of Figure, it defines a method of DRAW. And derived a Circle and Rectangle class, in the derived class, you have overwritten the Draw method, you may have a function, it wants to use a parameter x, and you can call $ x-> DRAW (). If you have polymorphism, which DRAW method calls depending on the type of object you passed to this function.

Polymorphism is in the interpretation language like PHP (imagine a C compiler to generate such code, which method you should call? You don't know what type of object you have, good, this is not a focus) is very Easy to natural. Therefore, PHP certainly supports polymorphism.

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

Function nicedrawing ($ x) {

/ / Assume this is a method of the Board class

$ x-> Draw ();

}

$ Obj = New Circle (3,187);

$ obj2 = New Rectangle (4, 5);

$ board-> nicedrawing ($ obj);

// will call the Circle's DRAW method

$ board-> nicedrawing ($ OBJ2);

// will call Rectangle's DRAW method

?>

Object-oriented programming with PHP

Some "Purists" may say PHP is not a real object-oriented language, which is a fact. PHP is a hybrid language that you can use OOP or use traditional procedural programming. However, for large projects, you may want / need to use pure OOP in PHP to declare class, and you only use objects and classes in your project.

As projects are getting bigger, using OOP may help, OOP code is easy to maintain, easy to understand and reuse. These are the foundation of software engineering. Applying these concepts in web-based projects will become the key to the success of future websites. PHP advanced OOP technology

After reading the basic OOP concept, I can show you more advanced technology:

Serializing

PHP does not support permanent objects, permanent objects in OOP are objects that hold status and function in a plurality of applications, which means that they have the ability to save objects to a file or database, and can load object later. . This is the so-called serialization mechanism. PHP has a serialization method that can be called by an object, and the serialization method can return a string representation of the object. However, serialization only saves the member data of the object without the method of private.

In PHP4, if you sequence the object to the string $ s, then release the object, then deserialize the object to $ OBJ, you can continue to use the object! I don't recommend this to do this, because this behavior can still be used in future versions in the document. (b) This may result in a misunderstanding, and when you save a serialized version to the disk and exit the script. When running this script later, you cannot expect the object's method to the object, because the string is not included in the method.

All in all, PHP serialization is very useful for the member variables of the saved object. (You can also serialize the associated arrays and arrays into a file).

Example:

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

$ OBJ = New classfoo ();

$ Str = Serialize ($ OBJ);

/ / Save $ STR to disk

// after a few months

// Load STR from disk

$ obj2 = unserialize ($ STR)

?> --------------------------------------------------------------------------------------------- ---

You have recovered member data, but it does not include methods (as mentioned by documentation). This leads to only the only way to access member variables (you don't have any other way!) By using $ OBJ2-> X, don't try it at home.

There are some ways to solve this problem, I stayed, because of this simple article, they are too bad.

I would rather happily all of the full-sequence characteristics in the subsequent versions of PHP.

Author: Luis Argerich

Original: www.code-labs.com

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

New Post(0)