/***************************************************** ****** Translation: midiguy Please point out any translation errors E-Mail: midiguy@263.net QQ: 5149927 ********************* **************************************/ The origin of object-oriented programming comes from the way people think about objects such as phones and cars. Many programmers like to use words like "packaging" or "inheritance" when discussing object-oriented programming, which confuse most people. We can relate the concept of object-oriented to natural objects to understand its principles. Let’s use transportation as an example. Blueprint In order to build a vehicle we need a blueprint. This blueprint can define the number, color, etc. of the vehicle's wheels. A vehicle is defined by certain attributes and behaviors. In PHP, these properties and behaviors are called variables and methods (functions). A set of variables and methods that describe an object constitutes a "class". Expanded Design Drawing Because there are different types of vehicles such as cars, bicycles and motorcycles. We need a method that allows us to add new functions to various types of transportation while also using the general method of transportation. In other words, because "crawlers" need to be used on all types of transportation, we There is no need to re-write this method. We can "inherit" to complete this function. If we create a class "car" that inherits from "vehicle", all methods in the "vehicle" class will be inherited by the "car" class. Abstraction The purpose of abstraction only focuses on part of the properties of a complex object. In order to solve your problem, you need to build a sophisticated object. You can easily get thousands of attributes for a car, but if you need to design a program to save a directory of car dealers, then you should only need a subset of 10 or so attributes. Such a car is abstracted into a car object suitable for programmatic use. Wrapping Wrapping can hide the contents of a set of methods from the tool mechanism and only provide the user with a well-defined interface. In object-oriented programming, packaging combines the data structures and methods of an object. The easiest way to understand "packaging" is by referring to a telephone. Consumers today can purchase a wide variety of telephones, and although the internal design of these telephones may differ, all of these telephone machines are capable of communicating through a standard common interface. This is the idea of packaging. A class can define new data types. PHP itself has variable types, such as string variables and floating point type variables. But through classes you can design your own data types such as ships, user reference manuals, databases, etc. A class defines the properties and behavior (member variables and methods) of this data type. The following example shows how to define a class containing properties and methods. Let's look at a practical example of our vehicle ----------Vehicle class------------ property1 = "Must be a Ford SUV"; } } function color($col) { $this->property2 = $col; } } ?> How to use the defined class After the class is defined, we can create an instance of it. To use the example just given, we create an instance of the "Vehicle" class below. setTires("Firestone"); ?> Using variables in a class The biggest difference is working with an object and the kinds of values the object has. A string variable is easy to understand because it has a value. $myStr = "PHP stands for... uh.. i forget"; However an object can have any kind of value $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 access to properties of an object, but if we want a variable to be private, we can do it in an emphatic way. $this pointer If you want to call a method of an object, you can use $this to call the member variables of the specified instance of this object. At first, you may not understand this. Let's look at an example. First, imagine you have two cars. $myCar = new vehicle(); $spousesCar = new vehicle(); Now you have two objects of the same class. You may have also heard the saying, "Now you have two composite 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 properties from this class. $myCar->property1; //These two are different $spousesCar->property1; //Even though property1 only appears once in the class definition. You must understand that it is only designed to form a new data type. But 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 works. When a specified object calls a function within a class, the object is automatically passed as a parameter. $this is used out of convenience. Take a look at the example below and you should be able to understand. $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 an instance of a class is created, if the developer wants to have a "default" function that can be called, this What to do? This is to use "constructor". In fact, you only need to simply define the name of the constructor as the name of the class to achieve it. Now every time you create an object of this class, this constructor method will be called. Inheritance of classes We have said before that a class can inherit another class; but how do we take advantage of this feature? In a system, many variables play the same role, but some have slightly different roles. Inheritance is very useful. Inheritance is a method by which a class can build itself using another class as a template. The inheriting class will inherit the member variables and methods defined in the inherited class. Classes that are extended or inherited are called subclasses. The class that is inherited is called the super class or parent class. This allows classes to function differently without affecting existing code. Now let's look at an example. class Airplane { var $tirePressure; var $fuelLevel; var $passengerLimit; function takeOff() { ... } function land() { ... } function preFlightCheck() {} } class sevenFortySeven extends Airplane { function preFlightCheck() { //747 aircraft takeoff preparation work} } class biplane extends Airplane { function preFlightCheck() { //Biplane takeoff preparation work} } $planeArray[] = new biplane(); $planeArray[] = new sevenFortySeven(); $planeArray [] = new sevenFortySeven(); $planeArray[] = new biplane(); for ($x = 0; $x preFlightCheck() ) { $currentPlane->takeOff(); //No matter what model it is The plane will know that it is about to take off} else { print "There is something wrong with the plane."; } } Static methods of a class When dealing with objects of a class, you may put a function that is useful for this object in the class instead of writing another special class. Such functions are called static methods. A good class should contain all useful (utility) functions. class Money { function addTax($amount,$percent){ return $amount + ($amount * $percent); } function convertCurrency ($amount, $from, $to) { //Find a conversion from $from in the database Conversion rate to $to return $amount * $rate; } } $total = Money::addTax($subtotal,6.5); $yen = Money::convertCurrency ($usd, "america", "japan"); method Factory (Factory Methods) Sometimes it is beneficial to break the code into chunks to create objects. You can use a large number of classes, or you can use a class to determine the object to use factory methods (factory methods). Factory classes can help you organize your code effectively. Typically, a factory class contains a relatively large conversion declaration and returns an instance of the appropriate object. Let's look at an example with a C-scanner. There is a basic Item class, but there are also many subclasses that can be called for various products (such as electronics, clothing...). Class Item { var $price; var $isTaxable; var $properties; function getNewItem($upc) { //Connect to the database //Find the type of $upc and put it into the $type variable //Find the properties of $upc 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 requiresSclae() { return false; } } while ( $upc = $scanner->next_code() ) { / /Suppose there is a scanner class $z = Item::getNewItem($upc); if ($z->requiresScale() ) { echo "Requires size!"; } $subtotal += $z->properties[" price"]; }