PHP object-oriented (vi) __set () __get () __isset () __unset () The use of _php basics

Source: Internet
Author: User
10.__set () __get () __isset () __unset ()-- application of four methods
In general, it is more logical to define the attributes of a class as private. However, the read of the property
and assignment operations are very frequent, so in PHP5, two function "__get ()" and "__set ()" are predefined to obtain
Fetch and assign its properties, and check the __isset () of the property and the Method "__unset ()" that deletes the property.
In the previous section, we set up and get methods for each of the attributes, and in PHP5 we provided a special
Methods for setting values and getting values, "__set ()" and "__get ()", which do not exist by default,
Instead, we add it to the class by hand, like the construction Method (__construct ()), which is added to the class to exist,
You can add these two methods in the following ways, and of course you can add them in your own style:
Code fragment
Copy Code code as follows:

__get () method to get private property
Private Function __get ($property _name) {
if (Isset ($this-> $property _name)) {
Return ($this-> $property _name);
}else {
return (NULL);
}
The __set () method is used to set private properties
Private Function __set ($property _name, $value) {
$this-> $property _name = $value;
}

__get () Method: This method is used to get the private member property value, there is a parameter, the parameter passed to the
The name of the member property, which returns the value of the property being obtained, does not need to be invoked manually, because we can also
This method is a private method, which is invoked automatically when a private property is obtained directly from the object. Because the private property has been
is encapsulated, it is not possible to get the value directly (for example: "Echo $p 1->name" So direct access is wrong),
But if you add this method to the class, you get the value directly from a statement that uses "Echo $p 1->name".
The __get ($property _name) method is automatically invoked and the property name is passed to the parameter $property_name, which returns the value of the private property we passed in through the internal execution of the method. If the member property is not encapsulated as private, the object
itself will not automatically invoke this method.
__set () Method: This method is used to set the value for the private member property, with two arguments, and the first argument is for you to
The property name for the set value, the second parameter is the value to be set for the property, and there is no return value. This method also does not need us
By hand, it can also be made private, which is automatically invoked when a private property value is set directly, the same property
The private has been encapsulated, if there is no __set () This method, is not allowed, such as:
$this->name= ' Zhangsan ', which can go wrong, but if you add __set to the class ($property _name, $value)
This method, when assigning a value directly to a private property, calls it automatically, passing the attribute such as name to
$property _name, the value to be assigned to "Zhangsan" to $value, through the implementation of this method, to achieve the assigned value of the mesh
Of If a member property is not encapsulated as private, the object itself does not automatically invoke this method. In order not to pass in an illegal
Value, you can also make a judgment in this method. Code is as follows: code fragment
Copy Code code as follows:

<?php
Class person{
The following are the member properties of the person, all of which are private members of the package
Private $name; Person's name
Private $sex; The gender of the person
Private $age; The age of the person
__get () method to get private property
Private Function __get ($property _name) {
echo automatically invokes this __get () method <br> when directly retrieving private property values;
if (Isset ($this-> $property _name)) {
Return ($this-> $property _name);
}else {
return (NULL);
}
}
The __set () method is used to set private properties
Private Function __set ($property _name, $value) {
echo automatically invokes this __set () method to assign a value to a private property <br> "When setting the value of a private property directly;"
$this-> $property _name = $value;
}
}
$p 1=new person ();
An action that assigns a value directly to a private property automatically calls the __set () method to assign the value
$p 1->name= "John";
$p 1->sex= "Male";
$p 1->age=20;
Gets the value of the private property directly, the __get () method is automatically called, and the value of the member property is returned
echo "Name:". $p 1->name. " <br> ";
echo "Gender:". $p 1->sex. " <br> ";
echo "Age:" $p 1->age. " <br> ";
?>

Program execution Results:
When the private property value is set directly, this __set () method is automatically invoked to assign a value to a private property when the private property value is set directly, the __set () method is automatically called to assign a value to the private property
This __set () method is automatically invoked to assign a value to a private property when the private property value is set directly
This __get () method is automatically invoked when a private property value is obtained directly
Name: John
This __get () method is automatically invoked when a private property value is obtained directly
Sex: Male
This __get () method is automatically invoked when a private property value is obtained directly
Age: 20
If the above code does not add the __get () and __set () methods, the program will go wrong because it cannot operate on the outside of the class private
There are members, and the code above is to help us directly access the encapsulated proprietary by automatically invoking the __get () and __set () methods
Member's.
__isset () Method: Before looking at this method, let's look at the application of the "isset ()" function, Isset () is a function to determine whether a variable is set, pass in a variable as a parameter, return TRUE if the passed variable exists, or return false.
So if you use the "isset ()" function outside of an object to determine if the member in the object is set, can
Use it? In two cases, if the members of the object are public, we can use this function to determine the membership
Sex, if it is a private member property, this function will not work, because the private has been encapsulated, the outside
Department is not visible. Then we can not
Use the "isset ()" function outside the object to determine if private member properties are set? Okay, you just
Adding a "__isset ()" method to the class is OK when using the "isset ()" function outside the class to determine
If the private member of the face is set, it automatically invokes the "__isset ()" Method inside the class to help us finish the exercise.
, the "__isset ()" method can also be made private. You can add the following code to the class:
Code fragment
Copy Code code as follows:

Private Function __isset ($NM) {
echo "Automatically invokes <br> when using the Isset () function outside the class to determine the private member $nm;
return Isset ($this-> $nm);
}

__unset () Method: Before we look at this method, we'll take a look at the "unset ()" function, "unset ()"
The function is to delete the specified variable and return True, and the argument is the variable to be deleted. So if in an object
External to delete the member properties inside the object with the "unset ()" function can be, but also in two cases, if a
You can use this function to delete the public property of an object outside the object if the member property in the object is public.
The member property of the object is private, and I use this function without permission to delete it, but again if you are in an object
With the "__unset ()" method, you can delete the object's private member properties outside of the object. In the object
After adding the "__unset ()" method, use the "unset ()" function outside the object to delete the private inside of the object
Member properties, the "__unset ()" function is automatically invoked to help us delete private member properties within an object, and this method can also be defined as private within the class. Add the following code to the object:
Code fragment
Copy Code code as follows:

Private Function __unset ($NM) {
echo "<br>" automatically invoked when using the unset () function outside the class to delete private members;
unset ($this-> $nm);
}

Let's look at a complete example:
Code fragment
Copy Code code as follows:

<?php
Class person{
The following are the member properties of the person
Private $name; Person's name
Private $sex; The gender of the person
Private $age; The age of the person
__get () method to get private property
Private Function __get ($property _name) {
if (Isset ($this-> $property _name)) {
Return ($this-> $property _name); else {
return (NULL);
}
}
}
The __set () method is used to set private properties
Private Function __set ($property _name, $value) {
$this-> $property _name = $value;
}
__isset () method
Private Function __isset ($NM) {
The echo "Isset () function, when determining private members, automatically invokes <br>";
return Isset ($this-> $nm);
}
__unset () method
Private Function __unset ($NM) {
echo "<br>" automatically invoked when using the unset () function outside the class to delete private members;
unset ($this-> $nm);
}
}
$p 1=new person ();
$p 1->name= "This was a person name";
When you use the Isset () function to determine a private member, the __isset () method is automatically invoked to help us complete, returning the result to True
Echo Var_dump (Isset ($p 1->name)). " <br> ";
echo $p 1->name. " <br> ";
When you delete a private member using the unset () function, the __unset () method is automatically invoked to help us complete, delete the name private property
unset ($p 1->name);
has been deleted, and there is no output for this line.
Echo $p 1->name;
?>

The output results are:
Isset () function to determine a private member, automatically call the
BOOL (TRUE)
This is a person name
Called automatically when the unset () function is used outside of a class to delete a private member.
__set (), __get (), __isset (), __unset () These four methods are all added to the object, when needed
Automatically called to complete the operation of private properties within an object outside of an object.

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.