PHP5 object-oriented explanation-(10) _ set () _ get () _ isset () _ unset () four methods

Source: Internet
Author: User

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 two functions, "_ get ()" and "_ set () "to obtain and assign values to its attributes, and check the" _ isset () "and the method" _ unset () "of 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:

The code is as follows: Copy code


// _ Get () is used to obtain private attributes.
Function _ get ($ property_name)
{  
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
    }  
}   
 
// _ Set () is used to set private attributes.
Function _ set ($ property_name, $ value)
{  
$ This-> $ property_name = $ value;

// _ Get () is used to obtain private attributes.
Function _ get ($ property_name)
{
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
    }
}

// _ Set () is used to set private attributes.
Function _ set ($ property_name, $ value)
{
$ This-> $ property_name = $ value;
}__

Get () method: This method is used to obtain the attribute value of a private member. A parameter 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 and is automatically called when private attributes are directly obtained. Because private attributes have been encapsulated, you cannot directly obtain the value (for example, "echo $ p1-> name ), 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.

_ 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 is automatically called when private property values are directly set. The private property is encapsulated. If no _ set () this method is not allowed, for example, "$ this-> name = 'hangsan', this will cause an error, but if you add _ set ($ property_name, $ value) this method is automatically called when a value is directly assigned to a private attribute, passing the attribute such as name to $ property_name, pass the value "zhangsan" to $ value. The value can be assigned through the execution of 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: Copy code

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.
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.
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 = 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. "<br> ";
Echo "gender:". $ p1-> sex. "<br> ";
Echo "age:". $ p1-> age. "<br> ";

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.
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.
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 = 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. "<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 a variable is set. Input a variable as a parameter, if the input variable exists, true is returned; otherwise, false is returned.

So if I use the "isset ()" function outside an object to determine whether the members in the object are set, can I 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 the "_ isset ()" method to the class. When the "isset ()" method is used 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: Copy code

Private function _ isset ($ nm)
{  
Echo "automatically called when the isset () function is used to determine the private member $ nm outside the class ";
Return isset ($ this-> $ nm );

Private function _ isset ($ nm)
{
Echo "automatically called when the isset () function is used to determine the private member $ nm outside the class ";
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, the "_ unset ()" function is automatically called to delete the private member attributes in 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: Copy code

Private function _ unset ($ nm)
{  
Echo "automatically called when the unset () function is used outside the class to delete private members ";
Unset ($ this-> $ nm );

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

Class Person
{
// The following are the member attributes of a person.
Private $ name; // The 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 ";
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; the output result is:

When the isset () function is used to determine a private member, it is automatically called.
Bool (true)
This is a person name

When the isset () function is used to determine a private member, it is automatically called.
Bool (true)
This is a person name when the unset () function is used outside the class to delete private members, the _ set (), _ get (), and _ isset () functions are automatically called (), _ unset ()

These four methods are added to the object and are automatically called as needed to perform operations on private attributes inside the object outside the 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.