/*************************************** * ******************* Translation: midiguy translation errors please also point out E-Mail: midiguy@263.netQQ: 5149927 *************************************** ********
/*************************************** * ******************* Translation: midiguy translation errors please also point out E-Mail: midiguy@263.net QQ: 5149927 *************************************** * *****************/the source of object-oriented programming comes from people's thoughts on telephone and automobile objects. Many programmers like to use "packaging" or "inheritance" when discussing object-oriented programming. We can associate the concept of object-oriented with natural objects to understand its principles. Let's take transportation as an example. A design drawing is required to build a vehicle. This design map defines the number and color of the vehicle wheels. A vehicle is defined by certain attributes and behaviors. In PHP, these attributes and behaviors are called variables and methods (functions ). A group of variables and methods describing an object constitute a "class ". Extended design diagram because there are various types of transportation, such as cars, bicycles and motorcycles. We need a method that allows us to add new features for a variety of transport while also using a general method of transport, in other words, because "tracks" are used on all types of vehicles, we do not need to rewrite this method. We can "inherit" to complete this function. If we create a class of "car" inherited from "vehicle", all methods in the "vehicle" class will be inherited by the "car" class. Abstract Abstraction only focuses on the partial nature of a complex object. to solve your problem, you need to build a sophisticated object. You can easily get thousands of attributes of a car, but if you need to design a program to save a catalog of car dealers, then you only need a subset that contains more than 10 attributes. In this way, a car is abstracted into a car object suitable for programming. packaging and packaging can hide the content tool mechanism of a group of methods and only provide users with a well-defined excuse. In object-oriented programming, packaging combines the data structures and methods of objects. For more information about the "packaging" method, see telephones. Today, consumers can purchase a variety of telephones. Although the internal design of these telephones may be different, all these telephone machines can communicate through a standard public interface. This is the idea of packaging. a class can define a new data type. PHP itself has variable types, such as string variables and floating-point variables. However, you can design your own data types, such as ships, user reference manuals, and databases. A class defines the attributes and behaviors of this data type (member variables and methods ). The following example shows how to define a class that contains attributes and methods. Let's take a look at the actual example of our transportation. ---------- Transportation tools ------------ Property1 = "Must be a Ford SUV" ;}} function color ($ col) {$ this-> property2 = $ col ;}}?> After defining a class, we can create its instance. To use the previous example, we will create an "vehicle" instance below. SetTires ("Firestone");?> The biggest difference between using variables in a class is the type of values owned by an object and an object. It is easy to understand a string variable because it has a value. $ MyStr = "PHP stands... uh .. I forget "; however, an object can have values of any type $ myCar-> year = 1988; $ myCar-> value = 813.77; $ myCar-> hasAirbag = false; $ myCar-> color = "periwinkle"; in PHP, all member variables of an object are public by default. There is no way to force the access attribute of an object, but if you want a variable to be a private variable, we can implement it in an emphasized way. $ This pointer if you want to call the method of an object, you can use $ this to call the member variables of the specified instance of this object. At the beginning, you may not understand this. let's look at an example. First, assume you have two cars. $ MyCar = new vehicle (); $ spousesCar = new vehicle (); now you have two objects of the same class. You may have heard this saying: "Now you have two comprehensive variables of the same user-defined variable type ". These are just different ways of talking about the same OOP concepts. each variable, $ myCar and $ spousesCar, has an independent set of attributes for this class. $ MyCar-> property1; // These two are different $ spousesCar-> property1; // even if property1 appears only once in the class definition. You must be aware that it is only designed to form a new data type. However, there is only one setTires () function in this class. When we use the following statement, how does it know who called it? $ MyCar-> setTires ("Firestone"); Haha, now $ this will work. When a specified object calls a function inside the class, this object is automatically passed as a parameter. $ This is used for convenience. You can understand the following example. $ MyCar-> setTires ("Firestone");/*** method */function setTires ($ type) {if ($ type = "Firestone ") {$ this-> property1 = "Must be a Ford SUV" ;}}// $ this represents the $ myCar variable $ spousesCar-> setTires ("Goodyear "); /*** method */function setTires ($ type) {if ($ type = "Firestone ") {$ this-> property1 = "Must be a Ford SUV" ;}}// now it represents $ spousesCar. create a constructor after a class instance is created, what should I do if the developer wants to have a "default" function to call? This is to use the constructor. In fact, you only need to simply define the constructor name as the class name. The constructor method is called every time you create an object of this class. Class inheritance we have said before that one class can inherit another class; but how can we use this function? In a system, many variables play the same role, but some of them have different roles. Inheritance is very useful. Inheritance is a method in which a class can use another class as a template to establish itself. The inherited class inherits the member variables and methods defined in the inherited class. Classes that are extended or inherited are called subclasses. The inherited class is called a superclass or parent class. In this way, the roles of classes are different and the existing code is not affected. now let's take a look at the example. Class Airplane {var $ tirePressure; var $ fuelLevel; var $ passengerLimit; function takeOff (){...} function land (){...} function preFlightCheck () {}} class sevenFortySeven extends Airplane {function preFlightCheck () {// 747 flight preparation} class biplane extends Airplane {function preFlightCheck () {// preparations for take-off of a dual-wing aircraft} $ planeArray [] = new biplane (); $ planeArray [] = new sevenFortySeven (); $ planeArra Y [] = new sevenFortySeven (); $ planeArray [] = new biplane (); for ($ x = 0; $ x <count ($ planeArray); $ x ++) {$ currentPlane = $ planeArray [$ x]; if ($ currentPlane-> preFlightCheck () {$ currentPlane-> takeOff (); // no matter what type of aircraft, it will all know that it is about to take off} else {print "The plane has a problem. ";}} class static method when processing a class object, you may put a function useful to this object in this class, instead of writing another special class. Such a function is called a static method. A good class should contain all useful (utility) functions. Class Money {function addTax ($ amount, $ percent) {return $ amount + ($ amount * $ percent);} function convertCurrency ($ amount, $ from, $) {// Find a conversion rate from $ from to $ to in the database. return $ amount * $ rate; }}$ total = Money: addTax ($ subtotal, 6.5 ); $ yen = Money: convertCurrency ($ usd, "america", "japan"); Factory Methods sometimes divides code into blocks to create objects. You can use a large number of classes, or use a class to determine the use of factory methods for objects ). The factory class helps you effectively organize your code. Generally, the factory class contains a large conversion declaration and returns an instance of an appropriate object. Let's take a look at the C scanner example. There is a basic Item class, but there are also many sub-classes that can be called for a variety of products (such as electronic, clothing ...). Class Item {var $ price; var $ isTaxable; var $ properties; function getNewItem ($ upc) {// connect to the database // find the $ upc type and put it in the $ type variable // find the $ upc attribute and put it in the $ attrib variable; return new $ type ($ attrib);} class produce extends Item {function produce ($ a) {$ this-> properties = $ a;} function requiresScale () {return true ;}} class hardlines extends Item {function hardlines ($ a) {$ this-> properties = $ a;} function requir EsSclae () {return false ;}} while ($ upc = $ assets-> next_code () {// assume that there is a scanner class $ z = Item :: getNewItem ($ upc); if ($ z-> requiresScale () {echo "size required! ";}$ Subtotal + = $ z-> properties [" price "];}