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:
<?PHP02//The __get () method is used to get the private property03function__get ($property _name)04 {05if(Isset ($ This-$property _name))06 {07return($ This-$property _name);08 }09Else10 {11return(NULL);12 }13 }14 15//The __set () method is used to set the private property16function__set ($property _name, $value)17 {18 $ This$property _name =$value;19}
__get () Method: This method is used to obtain 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 called automatically 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 has been encapsulated in private, if there is no __set () This method is not allowed, such as: "$this->name=" Zhangsan ', this will make a mistake, 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. The value to be assigned "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:
<?PHP02class Person03 {04//The following are the member properties of a person, both encapsulated private membersPrivate $name;//the name of the man$sex private;//Man's Sex$age private;//The age of the person08 09//The __get () method is used to get the private property10function__get ($property _name)11 {echo "The __get () method is automatically called when the value of the private property is obtained directly <br/>";13if(Isset ($ This-$property _name))14 {15return($ This-$property _name);16 }17Else18 {19returnNULL;20 }21st }22 23//The __set () method is used to set the private property24function__set ($property _name, $value)25 {echo "Automatically calls this __set () method to assign a value to a private property <br/>" When setting the value of a private property directly;27 $ This$property _name =$value;28 }29 }$p 1 =NewPerson ();32 33//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;37 38//gets the value of the private property directly, automatically calls the __get () method, returns the value of the member propertyecho "Name:". $p 1->name. "<br/>";echo "Gender:". $p 1->sex. "<br/>";echo "Age:". $p 1->age. "<br/>";?>
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:
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:
1 function __isset ($nm)2 {3 echo "When you use the Isset () function outside a class to measure a private member $nm, the <BR is automatically called > "; 4 5 return isset ($$nm); 6 }
__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:
1 function __unset ($nm)2 {3 echo "When you use the unset () function outside of a class to delete a private member, the <BR is called automatically. > "; 4 unset ($$nm); 5 }
Let's look at a complete example:
<?PHP02class Person03 {04//The following is the person's member propertyPrivate $name;//the name of the man$sex private;//Man's Sex$age private;//The age of the person08 09//The __get () method is used to get the private propertyPrivatefunction__get ($property _name)11 {12if(Isset ($ This-$property _name))13 {14return($ This-$property _name);15 }16Else17 {18returnNULL;19 }20 }21 22//The __set () method is used to set the private propertyAll privatefunction__set ($property _name, $value)24 {25 $ This$property _name =$value;26 }27 28//__isset () methodPrivatefunction__isset ($NM)30 {The echo "Isset () function automatically calls <br/> When measuring a private member";32returnIsset ($ This-$nm);33 }34 35//__unset () methodPrivatefunction__unset ($NM)37 {"When you use the unset () function outside a class to remove a private member, the <BR/> is called automatically";Unset ($ This-$nm);40 }41 }1 $p =NewPerson ();$p 1->name = "This was a person name";45 46//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 TrueEcho Var_dump (Isset ($p 1->name)). "<br >";Echo $p 1->name. "<br/>";49 50//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 propertyWuyi unset ($p 1->name);52 53//has been removed, there is no output for this lineEcho $p 1->name;?>
The output is:
the Isset () function automatically calls when a private member is measured
Boolean true
This was a person name
Automatically called when a private member is removed by using the unset () function outside the class.
the Isset () function automatically calls when a private member is measured
__set (), __get (), __isset (), __unset () are all four methods that we add to the object and are automatically called when needed to complete the operation of the object's internal private property outside the object.
Finally, additional notes:
1, __set (), __get () is specifically for the private properties of the class (private, protected), for the public property of the class, the outside can be directly accessed and set (such as: $p 1->name), that is, do not go __set (), __ Get () Function!!!
2, PHP5.3 and later, the above magic Methods (__get (), __set (), __isset (), __unset (), etc.) advocated are of the public type, and are not static method, otherwise a warning message will be given!
<? PHPQianyunlai class A( Private $name = ' A '; Public $old = ' + '; thefunction __get ($name) , $name, ' <br/> ' ; Ten return $$name; new A (); Print_r ($a,name); ?>
Php--opp--10.__set (), __get (), __isset (), __unset () Four ways to apply