Object-oriented programming in PHP: Measures to large PHP projects

xiaoxiao2021-03-06  72

This article describes the object-oriented programming (OOP) of PHP. I will demonstrate how to use fewer code but better programs with object-oriented concepts. I wish you all good luck. Object-oriented concepts have different opinions on every author, I will remind one of the object-oriented languages ​​should have anything: - Data Abstraction and Information Hide - Inheritance - Multi - Polymorphism Using Categories in PHP Packaging : x = $ v;} Function getX () {RETURN $ this-> x;}}?> Of course you can use your own way, but there is a standard always good. Data members in PHP use "VAR" definition, and the data member is not type until it is assigned. A data member may be an Integer, an array, a federated array or even object (Object). Method is defined in the class, to access data members in the method, you must use $ this-> name this method Otherwise, the method is a partial variable of a function. Use New to create a object $ OBJ = New Something; then use member functions $ OBJ-> setX (5); $ see = $ obj-> getx (); setx member function will 5 assign 5 Obj (instead) OBJ Member variables, then getx return value 5. You can also use object reference to access member variables, for example: $ OBJ-> X = 6; however, this is not a good object-oriented method. I insist that you should use the member function to set the value of the member variable and read the member variable through the member function. If you think member variables are unsable to use a Member function, you will become a good object-oriented programmer. But unfortunately, PHP itself has no way to state that a variable is private, so the bad code is allowed. Inherit the use of Extend to declare in PHP. Y = $ V; } Function getY () {RETURN $ THIS-> Y;}}?> The object of such "Another" has the member variables and method functions of the parent class, plus their own member variables and member functions. Such as: $ obj2-> setx (6); $ obj2-> sety (7); multiple inheritance is not supported, so you can't let a class inherit multiple classes.

In the inheritance class, you can redefine the way to redefine the method if we redefine getX in "Another", then we can no longer access the member functions in "Something". Similarly, if we declare one and The parent class is the member variable of the same name, then the inherited class variable will hide the same name variable of the parent class. You can define a class constructor, constructor is a member function that is the same name, and is called when you create a class. x = $ y;} function setX ($ v) {$ this-> x = $ v;} Function getX () {Return $ this-> x;}}?>

So you can create objects with the following method: $ obj = new something (6); Constructor Auto assigning 5 to member variables x, constructor, and member functions are ordinary PHP functions, so you can use the default parameters. Function Something ($ x = "3", $ y = "5") then: $ obj = new thing "; // x = 3 and y = 5 $ obj = new Something (8); // x = 8 AND Y = 5 $ OBJ = New Something (8, 9); // x = 8 and y = 9 Definition method of default parameters is the same, so you can't pass a value to Y but let X to get default value, The transmission of the arguments is from left to right, and the function will use the default parameter when there is no more real parameters. Only when the inherited structure is called, the inherited class object is created, the parent class constructor is not called, which is the characteristics of PHP different other object-oriented languages, because the constructor call chain is the characteristic of object-oriented programming . If you want to call the constructor of the base class, you have to explicitly call it in the constructor of the inherited class. This work is because the method of the parent class in the inheritance class is available. y = 5; $ this-> Something (); // expressit call to base class constructor.}?> In object-oriented programming, a good mechanism is using an abstract class Abstract classes are classes that cannot be instantiated but used to define interfaces to inherited class. Designers often use abstract classes to enforce programmers to inherit programmers can only be inherited from a particular base class, so they can determine the new classes have the features required, but there is no standard approach in PHP to do this, but: if you are The definition base class is required, which can be called "DIE" in the constructor so you can make sure it can't instantiate, now define the function of the abstract class and call "Die" in each function, if inherited The middle programmer does not want to redefine and directly call the function of the base class, will generate an error. In addition, you need to be sure that because there is no type of PHP, some objects are created from the inheritance class from the base class, so add a method to identify classes in the base class (return "some identity") and verify this, when you receive Go to an object as a parameter. However, it is useless for a villager program because he can define this function in inherited class, usually this way only for lazy programmers. Of course, the best way is to prevent the program from accessing the code to provide an interface. Heavy load is not supported in PHP. Object-oriented programming You can overload a member function by defining different parameters and how much. PHP is a loose type language, so the parameter type overload is not used, and the number of different parameters is different. Sometimes, overloading constructors in object-oriented programming is useful, so you can create different objects in different ways (by passing different parameters).

A small door can do this: $ name (); // Note That $ this -> $ name () IS Usually Wrong But Here // $ Name Is A String With The Name of The Method to Call.} Function Myclass1 ($ x) {code;} Function Myclass2 ($ x, $ y) {code;}}?>

This approach can be partially reached for the purpose of overload. $ obj1 = new myclass (1); // Will Call myclass1 $ obj2 = new myclass (1, 2); // will call myclass2 feeling not bad! Polymorphisms are defined for the ability to determine the ability to call the method when an object is passed as a parameter during the run time. For example, use a class definition method "DRAW", inherited the behavior of "DRAW" to draw circles or squares so that you have a function of a parameter X-> DRAW (), in the function. If you support polymorphism, the call to the "DRAW" method depends on the type of object X. Polymorphism is naturally supported in PHP (I want to think about this situation if it is compiled in the C compiler, however, you don't know what the object is, of course, not this situation). Fortunately, PHP supports polymorphism. Nicedrawing ($ OBJ); // Will Call The Draw Method of Circle. $ Board-> NiceDrawing ($ OBJ2); // Will Call The Draw Method of Rectangle.?> PHP object-oriented programming pure object The agency believes that PHP is not a real object-oriented language, which is right. PHP is a mixed language that you can use it with object or traditional structure programming. For large works, you may or requires the use of pure object-oriented methods to define classes and use only objects and classes in your project. Agrective project will benefit from object-oriented methods, and object-oriented projects are very easy to maintain, easy to understand and reuse. This is the basics of software engineering. Using these concepts is the key to success in the website design. Advanced Objective Technology in PHP After reviewing the object-oriented basic concept, I will introduce some more advanced technology. Serialized PHP does not support persistence objects, in an object-oriented language, persistent objects are some objects that have multiplexed multiple calls that still maintain their status and function, which means that there is a saving object to file or database then Reload the object. This mechanism is called serialization. PHP has a serialization function that can be called in the object, and the serialization function returns a string representing this object. The serialization function is then saved is a member data instead of a member function. In PHP4, if you serialize an object to a string $ S, then remove this object, then configure the object to $ OBJ, you can still call the object's method function. But I don't recommend this method, because (a) This function is not necessarily supported in the future (b) This leads to an illusion, if you save a serialized object to disk and exit the program. When you re-run this script, you cannot connect to this object and want the object's method function. Because the serialized string does not represent any member functions. Finally, the member variable of serial saving object is very useful in PHP, just this. (You can serialize the joint array and arguments into the disk). example:

In the above example, you can restore member variables without member functions (according to documentation). This results in $ OBJ2-> X is the only way to access member variables (because there is no member function). There are still some ways to solve this problem, but I will leave you because it will get dirty. I hope PHP will fully support serialization in the future. Using classes to manipulate saved data PHP and object-oriented programming a better place is that you easily define classes to manipulate some things, and call the appropriate classes when needed. Suppose there is an HTML file, you need to select a product by selecting the ID number of the product, your data is saved in the database, and you want to display product information, such as prices, etc. You have different kinds of products, the same actions have different meanings for different products. For example, a sound means playing it, and for other products, it may be a picture stored in the database. You can use object-oriented programming and PHP to reach, code is less but better. Define a class, define the method you should have, then define each product class (SoundItem class, viewableItem class, etc.), redefine each product class method, so that you need. Define a class to each product type according to the product type field you save in the database, a typical product table should have a field (id, type, price, description, etc.). Get the type information from the table in the script, then instantiate the corresponding class object: action ();?>>

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

New Post(0)