PHP5 Object-oriented detail-(x) __set () __get () __isset () __unset () Four Methods _php tutorial

Source: Internet
Author: User
This article brief introduction about PHP5 object-oriented detailed-(ten) __set () __get () __isset () __unset () Four methods, there is a need to send a friend can refer to the solution.

Application of four methods of __set () __get () __isset () __unset ()
In general, it is more realistic logic to always define the properties of a class as private. However, the read and assign operations for the properties are very frequent, so in PHP5, two functions "__get ()" and "__set ()" are predefined to get and assign their properties, as well as to check the Properties ' __isset () and delete the properties "__unset ()".

In the previous section, we set and get a method for each property, and in PHP5 we were given a way to set the value and get the value specifically for the property, "__set ()" and "__get ()", two methods that were not by default, but that we manually added to the class, like the construction method ( __construct ()), the class is added in order to exist, you can add the two methods as follows, of course, can also be added according to personal style:

code as follows copy code


//__get () method to get the private property
Function _ _get ($property _name)
{
if (isset ($this, $property _name)) {
return ($this $property _name);
}else {
return (NULL); The
}
}

//__set () method is used to set the private property
function __set ($property _name, $value)
{
$this, $property _name = $value; The
}

//__get () method is used to get the private property
function __get ($property _name)
{
if (isset ($this, $property _name)) {
Return ($this, $property _name);
}else {
return (NULL);
} The
}

//__set () method is used to set the private property
function __set ($property _name, $value)
{
$this, $property _name = $ Value
}__

Get () Method: This method is used to get the value of the private member property, there is a parameter, the parameter passed in the name of the member property you want to get, return the obtained property value, this method does not need to be called by us, it is automatically called when we get the private property directly. Because the private property is already encapsulated, it is not possible to get the value directly (for example: "Echo $p 1->name"), but if you add this method to the class, use "Echo $p 1->name" When such a statement gets the value directly, it automatically calls the __get ($property _name) method, passes the property name to the parameter $property_name, and returns the value of our incoming private property through the internal execution of the method.

__set () Method: This method is used to set the value of the private member property, there are two parameters, the first parameter is you want to set the value of the property name, the second parameter is to set the value of the property, there is no return value. This method does not have to be called manually, it is automatically called when the value of the private property is set directly, the same property private has been encapsulated, if there is no __set () This method is not allowed, such as: "$this->name= ' Zhangsan ', this will be wrong, But if you add __set ($property _name, $value) to the class, it is automatically called when you assign a value directly to the private property, and the attribute, such as name, is passed to $property_name, and the value to be assigned is "Zhangsan" to $value, through the execution of this method, to achieve the purpose of assignment, in order not to pass the illegal value, but also in this method to make a judgment. The code is as follows:

The code is as follows Copy Code

Class Person
{
The following are the member properties of a person, both encapsulated private members
Private $name; The name of the man
Private $sex; Man's Sex
Private $age; The age of the person

The __get () method is used to get the private property
function __get ($property _name)
{
echo "Automatically calls this __get () method when it directly acquires the value of a private property.
";
if (Isset ($this-$property _name)) {
Return ($this, $property _name);
}else {
return (NULL);
}
}

The __set () method is used to set the private property
function __set ($property _name, $value)
{
echo "Automatically calls this __set () method to assign a value to a private property when setting the value of a private property directly
";
$this $property _name = $value;
}
}

$p 1=new person ();

An operation that assigns a value directly to a private property, automatically calls the __set () method to assign the value
$p 1->name= "Zhang San";
$p 1->sex= "Male";
$p 1->age=20;

Gets the value of the private property directly, automatically calls the __get () method, returns the value of the member property
echo "Name:". $p 1->name. "
";
echo "Gender:". $p 1->sex. "
";
echo "Age:" $p 1->age. "
";

Class Person
{
The following are the member properties of a person, both encapsulated private members
Private $name; The name of the man
Private $sex; Man's Sex
Private $age; The age of the person

The __get () method is used to get the private property
function __get ($property _name)
{
echo "Automatically calls this __get () method when it directly acquires the value of a private property.
";
if (Isset ($this-$property _name)) {
Return ($this, $property _name);
}else {
return (NULL);
}
}

The __set () method is used to set the private property
function __set ($property _name, $value)
{
echo "Automatically calls this __set () method to assign a value to a private property when setting the value of a private property directly
";
$this $property _name = $value;
}
}

$p 1=new person ();

An operation that assigns a value directly to a private property, automatically calls the __set () method to assign the value
$p 1->name= "Zhang San";
$p 1->sex= "Male";
$p 1->age=20;

Gets the value of the private property directly, automatically calls the __get () method, returns the value of the member property
echo "Name:". $p 1->name. "
";
echo "Gender:". $p 1->sex. "
";
echo "Age:" $p 1->age. "
";

Program execution Results:
The __set () method is automatically called when a private property value is set directly to assign a value to a private property
The __set () method is automatically called when a private property value is set directly to assign a value to a private property
The __set () method is automatically called when a private property value is set directly to assign a value to a private property
This __get () method is called automatically when a private property value is obtained directly
Name: Zhang San
This __get () method is called automatically when a private property value is obtained directly
Gender: Male
This __get () method is called automatically 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 the private members cannot be manipulated outside the class, and the above code is automatically called by the __get () and __set () methods to help us directly access the encapsulated private members.

__isset () Method: Before looking at this method, we 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, or return TRUE if the passed-in variable exists, or return false.

So what if the "isset ()" function is used outside an object to determine if the members of the object are set, can it be used? In two cases, if the members of the object are public, we can use this function to determine the member properties, if it is a private member property, this function will not work, because the private is encapsulated, is not visible externally. Then we can not use the "isset ()" function outside the object to determine whether the private member property is set? Yes, you can just add a "__isset ()" method to the class, and when you use the "isset ()" function outside of the class to determine whether a private member of the object is set, the "__isset ()" Method inside the class is automatically invoked to help us do this, "__ The Isset () "method can also be made private. You can add the following code to the class:

copy code

Private function __isset ($nm)
{
Echo When the Isset () function is used outside the class to determine the private member $NM, it is called automatically;
Return Isset ($this-$NM);
}

Private function __isset ($nm)
{
echo] automatically called when the Isset () function is used outside the class to measure private member $nm ";
return Isset ($this- > $nm);
}

__unset () Method: Before looking at this method, let's take a look at the "unset ()" function, the function of "unset ()" is to delete the specified variable and return True, the argument is the variable to be deleted. So if it is possible to delete the member property inside an object outside of an object with the "unset ()" function, it is also in two cases, if the member property inside an object is public, you can use this function to delete the public property of the object outside the object, if the object's member property is private, I don't have permission to delete this function, but also if you add "__unset ()" To an object, you can delete the object's private member properties outside the object. After adding the "__unset ()" method to the object, it automatically calls the "__unset ()" function to remove the private member property inside the object when using the "unset ()" function outside the object to delete the property of the private members 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 Copy Code

Private Function __unset ($NM)
{
echo "Automatically called when using the unset () function outside of a class to delete a private member";
Unset ($this, $nm);
}

Private Function __unset ($NM)
{
echo "Automatically called when using the unset () function outside of a class to delete a private member";
Unset ($this, $nm);
Let's look at a complete example:


Class Person
{
The following is the person's member property
Private $name; Man's name
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the 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 the private property
Private Function __set ($property _name, $value)
{
$this $property _name = $value;
}
__isset () method
Private Function __isset ($NM)
{
The echo "Isset () function is automatically called when determining the private member;
Return Isset ($this-$NM);
}
__unset () method
Private Function __unset ($NM)
{
echo "Automatically called when using the unset () function outside of a class to delete a private member";
Unset ($this, $nm);
}
}
$p 1 = new person ();
$p 1->name = "This was a person name";
When using the Isset () function to measure a private member, the __isset () method is automatically called to help us complete and return the result to True
Echo Var_dump (Isset ($p 1->name)). "";
Echo $p 1->name. "";
When you use the unset () function to delete a private member, the __unset () method is called automatically to help us complete, removing the name private property
unset ($p 1->name);
has been removed, there is no output for this line
Echo $p 1->name;

Class Person
{
The following is the person's member property
Private $name; Man's name
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the 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 the private property
Private Function __set ($property _name, $value)
{
$this $property _name = $value;
}
__isset () method
Private Function __isset ($NM)
{
The echo "Isset () function is automatically called when determining the private member;
Return Isset ($this-$NM);
}
__unset () method
Private Function __unset ($NM)
{
echo "Automatically called when using the unset () function outside of a class to delete a private member";
Unset ($this, $nm);
}
}
$p 1 = new person ();
$p 1->name = "This was a person name";
When using the Isset () function to measure a private member, the __isset () method is automatically called to help us complete and return the result to True
Echo Var_dump (Isset ($p 1->name)). "";
Echo $p 1->name. "";
When you use the unset () function to delete a private member, the __unset () method is called automatically to help us complete, removing the name private property
unset ($p 1->name);
has been removed, there is no output for this line
echo $p 1->name; output as:

The Isset () function automatically calls when a private member is measured
BOOL (TRUE)
This was a person name

The Isset () function automatically calls when a private member is measured
BOOL (TRUE)
This was a person name __set (), __get (), __isset (), __unset () when using the unset () function to remove a private member outside of a class

These four methods are all added to the object, and are called automatically when needed to complete the operation of the object's internal private property outside the object.

http://www.bkjia.com/PHPjc/629197.html www.bkjia.com true http://www.bkjia.com/PHPjc/629197.html techarticle This article brief introduction about PHP5 object-oriented detailed-(ten) __set () __get () __isset () __unset () Four methods, there is a need to send a friend can refer to the solution. __set () __get () __i ...

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