PHP class and object translation midiguy_php tutorials

Source: Internet
Author: User
/********************************************************** translation: Midiguy translation error points also please point out e-mail:midiguy@263.net QQ : 5149927 ***********************************************************/Object-oriented programming comes from the idea that people look at things such as telephones and cars. Many programmers like to use "wrapping" or "inheriting" words that confuse the average person when discussing object-oriented programming. We can relate the object-oriented concept to a natural object to understand its principles. Let's take a vehicle for example. Design diagram in order to build a vehicle we need a plan. This design can define the number, color, and so on 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." The extended design is due to a variety of different types of vehicles, such as automobiles, bicycles and motorcycles. We need a method that allows us to add new functionality to various modes of transport while also using a general method of transportation, in other words, since "crawler" is used on all types of vehicles, we do not need to rewrite this method. We are able to "inherit" to complete this function. If we create a class of "Cars" inherited from "vehicles", all the methods in the "Transport" class will be inherited by the "Car" class. Abstract abstract purpose only focus on a complex object part of the nature, in order to solve your problem requires you to build a sophisticated object. You can easily get thousands of properties for a car, but if you need to design a program to save a car dealership directory, you should only need a subset of more than 10 attributes. Such a car is abstracted into a car object suitable for programming use. Packaging can hide a set of methods of content tool mechanisms and only provide users with a well-defined excuse. In object-oriented programming, packaging makes the object's data structure and methods combined. The easiest way to understand "packaging" is to refer to the telephone. Today's consumers can buy a wide variety of telephones, although the internal design methods of these telephones may be different, but all of 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 type variables. But through the classes you can design your own data types such as ships, user reference manuals, databases, and so on. A class defines this data typeProperties and behaviors (member variables and methods). The following example shows how to define a class that contains properties and methods. Let's take a look at the actual examples of our vehicles ----------Transportation------------ Property1 = "must be a Ford SUV"; }} function Color ($col) {$this->property2 = $col;}} ?> How to use a defined class class definition, we can create an instance of it. To use the example that we have just made, we create an instance of the "Transport" class below. Settires ("Firestone");?> the biggest difference between using a variable in a class is the kind of value that you have with an object and an object. 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 an object's Access property, but if you want a variable to be a private variable, we can do it in an emphatic way. $this pointer if you want to invoke a method of an object, you can use $this to invoke this object to specify a member variable for the instance. At first, you may not understand this, let's look at an example. First, let's say you have two cars. $myCar = new Vehicle (); $spousesCar = new Vehicle (); Now you have two objects of the same class. You may have heard the saying, "Now you have two synthetic variables of the same user-defined variable type." These is just different ways of talking about the same OOP concepts. Each variable, $myCar and $spousesCar, has a separate set of properties 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 designed only to make up a new data type. But there is only one settires () function within this class. When we use the following statement, how does it know who called it? $myCar->settires ("Firestone"); Oh, now this $this is working. When a specified object invokes a function inside the class, the object is automatically passed as a parameter. The use of $this is for the convenience of the need. Take a look at the following example 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 $myCar variable $spousesCar->settir ES ("Goodyear"); /* * * method */function Settires ($type) {if ($type = = "Firestone") {$this->property1 = "must be a Ford SUV";}}//Now it's on behalf of Table $spousesCar. Create a constructor when an instance of a class is created, what if the developer wants to have a "default" function that can be called? This is to use the "constructor" in fact, you just need to simply define the name of the constructor as the name of the class can be implemented. Now every time you create an object of this class, the constructor method is called. Inheritance of classes as we have said before, a class can inherit another class; But how do we take advantage of it? In a system, many variables play the same role, and only some of the functions are somewhat different, and inheritance is very useful. Inheritance is a method by which a class can use another class as a template to establish itself. Inheriting classes inherit the member variables and methods that are defined in the inherited class. Classes that extend or inherit are called subclasses. The class that is inherited is called a superclass or a parent class. This allows the classes to function differently and does not affect 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 take-off preparations}} CL Biplane extends Airplane {function Preflightcheck () {//dual-wing aircraft takeoff preparations}} $planeArray [] = new biplane (); $planeArray [] = new Sevenfortyseven (); $planeArray[] = new Sevenfortyseven (); $planeArray [] = new biplane (); for ($x = 0; $x < count ($planeArray), $x + +) {$currentPlane = $planeArray [$x]; if ($currentPlane->preflightcheck ()) {$currentPlane->takeoff ();//whatever the model of the aircraft, it will know that it is going to take off} else {print "There is something wrong with the plane.";}} static method of a class when dealing with an object of a class, you might put a function that is useful to the object in this class, rather than writing another special class. Such a function is called a static method. A good class should contain all the useful (utility) functions. Class Money {function AddTax ($amount, $percent) {return $amount + ($amount * $percent),} function ConvertCurrency ($amoun T, $from, $to) {//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"); Method Factory (Factory Methods) Sometimes it's good to break code into one block to create objects. You can use a large number of classes, or you can use a class to determine objects using the factory class (factory methods). The factory class can help you organize your code effectively. In general, the factory class contains a larger conversion declaration and returns an instance of the appropriate object. Let's look at an example of a C scanner. There is an item basic class, but there are also many subclasses, these subclasses can be called for a variety of products (such as the electronic class, clothing class ...) )。 Class Item {var $price; var $isTaxable; var $properties; function Getnewitem ($UPC) {//Connect to Database//Find $UPC type and put it in $type variable// Find $The properties of the UPC and put it as a $attrib variable; return new $type ($attrib); }} class produce extends Item {function produce ($a) {$this->properties = $a;} function Requiresscale () {return TR Ue }} class Hardlines extends Item {function hardlines ($a) {$this->properties = $a;} function Requiressclae () {retur n false; }} while ($UPC = $scanner->next_code ()) {//Suppose there is a class of scanner $z = Item::getnewitem ($UPC); if ($z->requiresscale ()) {E Cho "needs size! "; } $subtotal + = $z->properties["Price"]; }

http://www.bkjia.com/PHPjc/532103.html www.bkjia.com true http://www.bkjia.com/PHPjc/532103.html techarticle /********************************************************** translation: Midiguy translation error points also please point out e-mail:midiguy@263.net QQ : 5149927 ******************************** ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.