_ Set _ get _ isset _ unset usage in php object-oriented systems _ PHP Tutorial

Source: Internet
Author: User
In php object-oriented language, _ set _ get _ isset _ unset usage is introduced. We often see the usage of bit _ set _ get _ isset _ unset in php object orientation, but we do not understand why we should use these items, next we will introduce one by one the usage of bit _ set _ get _ isset _ unset in php's object-oriented interface, but I don't understand why I want to use these things. let's introduce them one by one.

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.

The code is as follows:

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 () method:This method is used to obtain the attribute values of private members. a parameter is used to input the name of the member attribute you want to obtain and return the obtained attribute values. this method does not need to be called manually, because we can also make this method a private method, which is automatically called when the private property is directly obtained. 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.

The _ set () method is not allowed, for example, $ this-> name = 'hangsan, 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:

The code is as follows:


Class Person


{


// 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.
";


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 the private property value is directly set, the _ set () method is automatically called to assign values to the private property.
";


$ This-> $ property_name = $ value;


}


}


$ P1 = new Person ();


// 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 ."
";


Echo "gender:". $ p1-> sex ."
";


Echo "age:". $ p1-> age ."
";


?>

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:

The code is as follows:

Private function _ isset ($ nm)


{


Echo "is automatically called when the isset () function is used outside the class to determine the private member $ nm.
";


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:

The code is as follows:

Private function _ unset ($ nm)


{


Echo "automatically called when the unset () function is used outside the class to delete private members
";


Unset ($ this-> $ nm );


}

Let's look at a complete example:


Class Person


{


// 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)


{


When the echo "isset () function is used to determine private members, it is automatically called.
";


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
";


Unset ($ this-> $ nm );


}


}


$ P1 = new Person ();


$ 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.


Echo var_dump (isset ($ p1-> name ))."
";


Echo $ p1-> name ."
";


// When the unset () function is used to delete a private member, the _ unset () method is automatically called to help us complete the deletion.


Unset ($ p1-> name );


// The row has been deleted and no output exists.


Echo $ p1-> name;


?>

Output result:
When the isset () function is used to determine a private member, it is automatically called.
Bool (true)
This is a person name
Automatically called when the unset () function is used outside the class to delete private members
The _ set (), _ get (), _ isset (), and _ unset () methods are all added to the object, it is automatically called as needed to perform operations on private attributes inside the object outside the object.


Usage _ get _ isset _ unset usage of these things, but it is hard to understand why these things are used. next we will introduce one by one...

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.