PHP object-oriented improvements

Source: Internet
Author: User

First look at the Code:
<? Php
Class StrictCoordinateClass {
Private $ arr = array (x => NULL, y => NULL );
Function _ construct (){
Print "StrictCoordinateClass is being created ";
Print "<br/> ";
}
Function _ destruct (){
Print "<br/> ";
Print "StrictCoordinateClass is being destroyed ";
}
Function _ get ($ property ){
If (array_key_exists ($ property, $ this-> arr )){
Return $ this-> arr [$ property];
} Else {
Print "Error: Cant read a property other than x & y ";
}
}
Function _ set ($ property, $ value ){
If (array_key_exists ($ property, $ this-> arr )){
$ This-> arr [$ property] = $ value;
} Else {
Print "Error: Cant write a property other than x & y ";
}
}
}
$ Obj = new StrictCoordinateClass ();
$ Obj-> x = 1;
Print $ obj-> x;
Print "<br/> ";
$ Obj-> n = 2;
Print "<br/> ";
Print $ obj-> n;
?>

Output result:
StrictCoordinateClass is being created
1
Error: Cant write a property other than x & y
Error: Cant read a property other than x & y
StrictCoordinateClass is being destroyed
_ Construct () and _ destruct () are equivalent to constructor in Java and destructor in C.
For the _ get and _ set parameters, see the following:
Reference: http://www.phpchina.com/html/54/26354-31906.html
Application of four methods:. _ set () _ get () _ isset () _ unset ()
In general, the class attribute is always defined as private, which is more in line with the actual logic. However, attributes are read and assigned frequently. Therefore, in PHP5, we predefine the two functions "_ get ()" and "_ set () to obtain and assign values to the attributes, and check the "_ isset ()" and the method "_ unset ()" for deleting the attributes ()".
In the previous section, we set and obtain each attribute. In PHP5, we provided a method specifically for setting and obtaining values for the attribute, "_ set () "and" _ get () "methods. These two methods do not exist by default, but are manually added to the class, like the constructor (_ construct (), the class will only exist after it is added. You can add these two methods in the following way. Of course, you can also add them in your own style:
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name ){
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
}
}
// _ Set () is used to set Private attributes.
Private function _ set ($ property_name, $ value ){
$ This-> $ property_name = $ value;
}
_ Get (): This method is used to obtain the attribute value of a private member. There is a parameter that is used to input the name of the member attribute you want to obtain and return the obtained attribute value, this method does not need to be manually called, because we can also make this method a private method, which is automatically called when the object directly obtains the private property. Because private attributes have been encapsulated, values cannot be obtained directly (for example, "echo $ p1-> name" is incorrect ), however, if you add this method to the class, when you use a statement such as "echo $ p1-> name" to directly obtain the value, it will automatically call _ get ($ property_name) to pass the attribute name to the parameter $ property_name. The value of the private attribute we passed in is returned through the internal execution of this method. If the member attributes are not encapsulated as private, the object itself will not automatically call this method.
_ Set () method: This method is used to set values for private member attributes. There are two parameters. The first parameter is the attribute name for the set value, the second parameter is the value to be set for the attribute, with no return value. This method does not need to be called manually. It can also be made private. It is automatically called when private property values are directly set. Similarly, the private property has been encapsulated.
If there is no _ set () method, it is not allowed, for example: $ this-> name = 'zhangsan, this will cause an error, however, if you add the _ set ($ property_name, $ value) method to the class, it will be automatically called when the private property is assigned a value directly, pass the attribute such as name to $ property_name, and pass the value "zhangsan" to $ value. This method is used to assign values. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in invalid values, you can also make a judgment in this method. The Code is as follows:
<? Php
ClassPerson {
// The following are the member attributes of a person, all of which are encapsulated private members.
Private $ name; // name of a person
Private $ sex; // gender of a person
Private $ age; // age of a person
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name ){
Echo "the _ get () method is automatically called when the private property value is directly obtained. <br> ";
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
}
}
// _ Set () is used to set Private attributes.
Private function _ set ($ property_name, $ value ){
Echo "when setting private property values directly, the _ set () method is automatically called to assign values to private properties. <br> ";
$ This-> $ property_name = $ value;
}
}
$ P1 = newPerson ();
// Operations that directly assign values to private properties automatically call the _ set () method to assign values.
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// Directly obtain the value of the private attribute. The _ get () method is automatically called to return the value of the member attribute.
Echo "name:". $ p1-> name. "<br> ";
Echo "Gender:". $ p1-> sex. "<br> ";
Echo "age:". $ p1-> age. "<br> ";
?>

Program Execution result:
When setting the private property value directly, the _ set () method is automatically called to assign values to the private property.
When setting the private property value directly, the _ set () method is automatically called to assign values to the private property.
When setting the private property value directly, the _ set () method is automatically called to assign values to the private property.
The _ get () method is automatically called when the private property value is obtained directly.
Name: James
The _ get () method is automatically called when the private property value is obtained directly.
Gender: male
The _ get () method is automatically called when the private property value is obtained directly.
Age: 20
If the above Code does not contain the _ get () and _ set () methods, the program will fail because private members cannot be operated on outside the class, the above code automatically calls the _ get () and _ set () methods to help us directly access the encapsulated private members.
_ Isset () method: Before reading this method, let's take a look at the application of the "isset ()" function. isset () is a function used to determine whether variables are set, if a variable exists, true is returned. Otherwise, false is returned. If you use the "isset ()" function outside an object to determine whether the Members in the object are set, can you use it? In two cases, if the Members in the object are public, we can use this function to determine the Member attributes. If the Member attributes are private, this function does not work, the reason is that the private object is encapsulated and invisible to the outside. So we cannot use the "isset ()" function outside the object to determine whether the private member attribute is set? Yes. You only need to add a "_ isset ()" method to the class. When using "isset ()" outside the class () "function to determine whether the private members in the object are set, the" _ isset () "method in the class will be automatically called to help us complete this operation, the "_ isset ()" method can also be made private. You can add the following code to the class:
Private function _ isset ($ nm ){
Echo "automatically called when the isset () function is used outside the class to determine the private member $ nm <br> ";
Return isset ($ this-> $ nm );
}
_ Unset () method: Let's take a look at the function "unset ()" and "unset () "This function is used to delete the specified variable and return true. The parameter is the variable to be deleted. Can I use the "unset ()" function to delete member attributes outside an object, if the member attribute in an object is public, you can use this function to delete the public attribute of the object outside the object. If the Member attribute of the object is private, I have no permission to delete this function, but if you add the "_ unset ()" method to an object, you can delete the private member attributes of an object outside the object. After the "_ unset ()" method is added to the object, when the "unset ()" function is used outside the object to delete the private member attribute inside the object, automatically call the "_ unset ()" function
We delete the private member attributes inside the object. This method can also be defined as private within the class. Add the following code to the object:
Private function _ unset ($ nm ){
Echo "automatically called when the unset () function is used outside the class to delete Private Members <br> ";
Unset ($ this-> $ nm );
}
Let's look at a complete example:
<? Php
ClassPerson {
// The following are the member attributes of a person.
Private $ name; // name of a person
Private $ sex; // gender of a person
Private $ age; // age of a person
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name ){
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
}
}
// _ Set () is used to set Private attributes.
Private function _ set ($ property_name, $ value ){
$ This-> $ property_name = $ value;
}
// _ Isset () method
Private function _ isset ($ nm ){
Echo "automatically called when the isset () function is used to determine Private Members <br> ";
Return isset ($ this-> $ nm );
}
// _ Unset () method
Private function _ unset ($ nm ){
Echo "automatically called when the unset () function is used outside the class to delete Private Members <br> ";
Unset ($ this-> $ nm );
}
}
$ P1 = newPerson ();
$ P1-> name = "this is a person name ";
// When the isset () function is used to determine a private member, the _ isset () method is automatically called to help us complete the measurement. The returned result is true.
Echovar_dump (isset ($ p1-> name). "<br> ";
Echo $ p1-> name. "<br> ";

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.