Construct destructor encapsulation in php object-oriented. The constructor declaration is the same as the Declaration of other operations, but its name must be _ construct (). encapsulation is to combine object attributes and services into an independent and identical unit, the constructor declaration is the same as that of other operations, but its name must be _ construct (), encapsulation is to combine object attributes and services into an independent unit and conceal the internal details of objects as much as possible.
Constructor and constructor
Constructor:
Most classes have a special method called constructor. When an object is created, it automatically calls the constructor, that is, when the new keyword is used to instantiate the object, the constructor is automatically called.
The constructor declaration is the same as that of other operations, but its name must be _ construct (). This is a change in PHP5. in previous versions, the name of the constructor must be the same as the class name, which can still be used in PHP5, but it is rarely used now, in this way, the constructor can be made independent of the class name. when the class name changes, you do not need to change the corresponding constructor name. For backward compatibility, if a class does not have a method named _ construct (), PHP will search for a method written in php4 with the same name as the class name.
Format: function _ construct ([parameter]) {...}
Only one constructor can be declared in a class. Instead, the constructor is called only once every time an object is created, so it is usually used to execute some useful initialization tasks. For example, assign an initial value to a property when creating an object.
The code is as follows: |
|
// Create a human Class Person { // The following are the member attributes of a person. Var $ name; // name of a person Var $ sex; // gender of a person Var $ age; // age of a person // Define a constructor parameter: name $ name, gender $ sex, and age $ age Function _ construct ($ name, $ sex, $ age) { // Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor $ This-> name = $ name; // The $ sex passed in through the constructor is used to assign the initial enable value to the member attribute $ this-> sex. $ This-> sex = $ sex; // Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor $ This-> age = $ age; } // How this person speaks Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } } // Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age. $ P1 = new Person ("zhang san", "male", 20 ); $ P2 = new Person ("Li Si", "female", 30 ); $ P3 = new Person ("Wang Wu", "male", 40 ); // The following method of accessing the $ p1 object $ P1-> say (); // The following method of accessing the $ p2 object $ P2-> say (); // The following method is used to access the $ p3 object. $ P3-> say (); Output result: My name is John. Gender: Male. my age is: 20. My name is Li Si Gender: Female my age is: 30 My name is: Wang Wu Gender: Male my age is: 40 // Create a human Class Person { // The following are the member attributes of a person. Var $ name; // name of a person Var $ sex; // gender of a person Var $ age; // age of a person // Define a constructor parameter: name $ name, gender $ sex, and age $ age Function _ construct ($ name, $ sex, $ age) { // Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor $ This-> name = $ name; // The $ sex passed in through the constructor is used to assign the initial enable value to the member attribute $ this-> sex. $ This-> sex = $ sex; // Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor $ This-> age = $ age; } // How this person speaks Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } } // Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age. $ P1 = new Person ("zhang san", "male", 20 ); $ P2 = new Person ("Li Si", "female", 30 ); $ P3 = new Person ("Wang Wu", "male", 40 ); // The following method of accessing the $ p1 object $ P1-> say (); // The following method of accessing the $ p2 object $ P2-> say (); // The following method is used to access the $ p3 object. $ P3-> say (); Output result: My name is John. Gender: Male. my age is: 20. My name is Li Si Gender: Female my age is: 30 My name is: Wang Wu Gender: Male my age is: 40 |
Destructor:
The constructor is opposite to the constructor. The Destructor is newly added to PHP5 and does not contain the Destructor in PHP4. The Destructor allows you to perform operations or complete some functions before destroying a class, such as closing a file and releasing a result set, the Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the destructor is called before the object is destroyed in the memory. Similar to the constructor name, the destructor name of a class must be _ destruct (). The Destructor cannot contain any parameters.
Format: function _ destruct (){......}
The code is as follows: |
|
// Create a human Class Person { // The following are the member attributes of a person. Var $ name; // name of a person Var $ sex; // gender of a person Var $ age; // age of a person // Define a constructor parameter: name $ name, gender $ sex, and age $ age Function _ construct ($ name, $ sex, $ age) { // Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor $ This-> name = $ name; // The $ sex passed in through the constructor is used to assign the initial enable value to the member attribute $ this-> sex. $ This-> sex = $ sex; // Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor $ This-> age = $ age; } // How this person speaks Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } // This is a destructor called before the object is destroyed. Function _ destruct () { Echo "goodbye". $ this-> name .""; } } // Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age. $ P1 = new Person ("zhang san", "male", 20 ); $ P2 = new Person ("Li Si", "female", 30 ); $ P3 = new Person ("Wang Wu", "male", 40 ); // The following method of accessing the $ p1 object $ P1-> say (); // The following method of accessing the $ p2 object $ P2-> say (); // The following method is used to access the $ p3 object. $ P3-> say (); Output result: My name is John. Gender: Male. my age is: 20. My name is Li Si Gender: Female my age is: 30 My name is: Wang Wu Gender: Male my age is: 40 Goodbye, James Goodbye, Li Si Goodbye Wang Wu |
Encapsulation
Encapsulation is one of the three main features of surface image object programming. encapsulation is to combine object attributes and services into an independent unit and conceal the internal details of objects as much as possible, contains two meanings: 1. combine all attributes of an object with all services to form an inseparable independent unit (that is, an object ). 2. information hiding, that is, hiding the internal details of objects as much as possible to form a boundary (or a barrier) for external entities, and retaining only limited external interfaces to associate them with external entities.
The encapsulation principle is reflected in the software: the internal data (attributes) of the object cannot be freely accessed for parts other than the object ), this effectively avoids the "cross-infection" caused by external errors, so that software errors can be localized, greatly reducing the difficulty of error detection and troubleshooting.
Let's explain it with an example. if a person's object has attributes such as age and salary, the attributes such as personal privacy do not want others to get them at will, if you do not use encapsulation, others can get it if they want to know it, but if you encapsulate it, others will not be able to get the encapsulated attribute unless you say it yourself, otherwise, no one else can get it. For example, personal computers have a password, so you don't want others to log on, copy and paste it on your computer. There is also an object like a person, the attributes of height and age, which can only be increased by oneself, and cannot be randomly assigned by others.
The code is as follows: |
|
// Use the private keyword to encapsulate attributes and methods: // Original member: Var $ name; // The name of the declarer Var $ sex; // declare the gender of a person Var $ age; // declare the person's age Function run (){.......} // Change to the encapsulation form: Private $ name; // encapsulate a person's name using the private keyword Private $ sex; // encapsulate the gender of a person using the private keyword. Private $ age; // encapsulate the age of a person using the private keyword. Private function run (){......} // Encapsulate the human walking method with the private keyword // Use the private keyword to encapsulate attributes and methods: // Original member: Var $ name; // The name of the declarer Var $ sex; // declare the gender of a person Var $ age; // declare the person's age Function run (){.......} // Change to the encapsulation form: Private $ name; // encapsulate a person's name using the private keyword Private $ sex; // encapsulate the gender of a person using the private keyword. Private $ age; // encapsulate the age of a person using the private keyword. Private function run (){......} // Encapsulate a person's walking method using the private keyword. note: As long as there are other keywords before the member attribute, remove the original keyword "var ". you can encapsulate a member (member attribute and member method) in private mode. The encapsulated members cannot be directly accessed outside the class, and only the objects can be accessed by themselves. The following code produces an error: Class Person { // The following are the member attributes of a person. Private $ name; // The name of a person, which is encapsulated by private Private $ sex; // the gender of a person, which is encapsulated by private Private $ age; // age of a person, which is encapsulated by private // How this person can speak Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } // The way this person can walk is encapsulated by private Private function run () { Echo "this person is walking "; } } // Instantiate a person's instance object $ P1 = new Person (); // Try to assign values to private attributes, and the result will be incorrect $ P1-> name = "James "; $ P1-> sex = "male "; $ P1-> age = 20; // An error occurs when you try to print a private property. Echo $ p1-> name .""; Echo $ p1-> sex .""; Echo $ p1-> age ."" // Try to print the private member method, and the result will be incorrect $ P1-> run (); Output result: Fatal error: Cannot access private property Person: $ name Fatal error: Cannot access private property Person: $ sex Fatal error: Cannot access private property Person: $ age Fatal error: Cannot access private property Person: $ name Fatal error: Call to private method Person: run () from context''
Class Person { // The following are the member attributes of a person. Private $ name; // The name of a person, which is encapsulated by private Private $ sex; // the gender of a person, which is encapsulated by private Private $ age; // age of a person, which is encapsulated by private // How this person can speak Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } // The way this person can walk is encapsulated by private Private function run () { Echo "this person is walking "; } } // Instantiate a person's instance object $ P1 = new Person (); // Try to assign values to private attributes, and the result will be incorrect $ P1-> name = "James "; $ P1-> sex = "male "; $ P1-> age = 20; // An error occurs when you try to print a private property. Echo $ p1-> name .""; Echo $ p1-> sex .""; Echo $ p1-> age ."" // Try to print the private member method, and the result will be incorrect $ P1-> run (); Output result: Fatal error: Cannot access private property Person: $ name Fatal error: Cannot access private property Person: $ sex Fatal error: Cannot access private property Person: $ age Fatal error: Cannot access private property Person: $ name Fatal error: Call to private method Person: run () from context'' |
From the instance above, we can see that private members cannot be accessed externally, because private members can only access the object themselves, for example, $ p1: the object wants to describe its private attributes and access the private attributes in the say () method. (No access control is added. The default value is public and can be accessed anywhere)
// This person can talk about his/her own private attributes and access the private method here.
The code is as follows: |
|
Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age." "; // You can also access private methods here // $ This-> run (); } // This person can talk about his/her own private attributes and access the private method here. Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age." "; // You can also access private methods here // $ This-> run (); } Because the member method say () is public, we can call the say () method outside the class and change the above code; Class Person { // The following are the member attributes of a person. Private $ name; // The name of a person, which is encapsulated by private Private $ sex; // the gender of a person, which is encapsulated by private Private $ age; // age of a person, which is encapsulated by private // Define a constructor parameter to assign values to the private attribute name $ name, gender $ sex, and age $ age. Function _ construct ($ name, $ sex, $ age) { // Assign the initial value to the private member attribute $ this-> name through the $ name passed in by the constructor $ This-> name = $ name; // The $ sex passed in through the constructor is used to assign the initial enable value to the private member attribute $ this-> sex. $ This-> sex = $ sex; // Assign the initial value to the private member attribute $ this-> age through the $ age passed in by the constructor $ This-> age = $ age; } // This person can talk about his/her own private attributes and access the private method here. Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age." "; } } // Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age. $ P1 = new Person ("zhang san", "male", 20 ); $ P2 = new Person ("Li Si", "female", 30 ); $ P3 = new Person ("Wang Wu", "male", 40 ); // The following method of accessing the $ p1 object $ P1-> say (); // The following method of accessing the $ p2 object $ P2-> say (); // The following method is used to access the $ p3 object. $ P3-> say (); Output result: My name is John. Gender: Male. my age is: 20. My name is Li Si Gender: Female my age is: 30 My name is: Wang Wu Gender: Male my age is: 40
Class Person { // The following are the member attributes of a person. Private $ name; // The name of a person, which is encapsulated by private Private $ sex; // the gender of a person, which is encapsulated by private Private $ age; // age of a person, which is encapsulated by private // Define a constructor parameter to assign values to the private attribute name $ name, gender $ sex, and age $ age. Function _ construct ($ name, $ sex, $ age) { // Assign the initial value to the private member attribute $ this-> name through the $ name passed in by the constructor $ This-> name = $ name; // The $ sex passed in through the constructor is used to assign the initial enable value to the private member attribute $ this-> sex. $ This-> sex = $ sex; // Assign the initial value to the private member attribute $ this-> age through the $ age passed in by the constructor $ This-> age = $ age; } // This person can talk about his/her own private attributes and access the private method here. Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age." "; } } // Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age. $ P1 = new Person ("zhang san", "male", 20 ); $ P2 = new Person ("Li Si", "female", 30 ); $ P3 = new Person ("Wang Wu", "male", 40 ); // The following method of accessing the $ p1 object $ P1-> say (); // The following method of accessing the $ p2 object $ P2-> say (); // The following method is used to access the $ p3 object. $ P3-> say (); |
Output result:
My name is John. Gender: Male. my age is: 20.
My name is Li Si Gender: Female my age is: 30
My name is: Wang wugender: Male my age is: 40 because the constructor is the default public method (do not set the constructor to private ), so you can access it outside the class, so that you can use the constructor to create an object. In addition, the constructor is also a function in the class, so you can use the constructor to assign an initial value to the private attribute. The method of "Say ()" is public by default, so you can access it outside and state its own private property.
From the above example, we can see that private members can only be used inside the class and cannot be directly accessed outside the class, but they have access permissions within the class, so sometimes we need to assign values to and read private attributes outside the class, that is, to provide some accessible interfaces to the class, the constructor in the above example is a value assignment form, however, the constructor only assigns a value when creating an object. if we want to assign a value to an existing object, if you also use the constructor to pass values, a new object is created, which is not an existing object. Therefore, we need to make some interfaces that can be accessed externally for private attributes. The purpose is to change and access the attribute values when an object exists, this can be done only for attributes that need to be changed by the outside. the attributes that do not want to be accessed by the outside do not use such interfaces, so as to achieve the purpose of encapsulation, all functions are completed by the object itself, providing as few operations as possible for the outside.
If an interface is provided for the class, you can provide the setting and obtaining methods for private attributes outside the class to operate on private attributes. for example:
The code is as follows: |
|
// Private property age Prvate $ age; // Provide a public age setting method for external users Function setAge ($ age) { // When assigning values to attributes, to avoid setting invalid values to attributes If ($ age <0 ||$ age> 130) Return; $ This-> age = $ age; } // Provide a public method for obtaining the age. Function getAge () { Return ($ this-> age ); } // Private property age Prvate $ age; // Provide a public age setting method for external users Function setAge ($ age) { // When assigning values to attributes, to avoid setting invalid values to attributes If ($ age <0 ||$ age> 130) Return; $ This-> age = $ age; } // Provide a public method for obtaining the age. Function getAge () { Return ($ this-> age ); } On |
You can set and obtain values for a member attribute. of course, you can also assign values and values to each attribute in the same way to complete access outside the class.
Encapsulation is to combine object attributes and services into an independent and identical unit...