Program execution results:
When setting the private attribute value directly, the __set() method is automatically called to assign a value to the private attribute
When setting the private attribute value directly, the __set() method is automatically called to assign a value to the private attribute
When setting the private attribute value directly, the __set() method is automatically called to assign a value to the private attribute
When directly obtaining the private attribute value, the __get() method is automatically called
Name: Zhang San
When directly obtaining the private attribute value, the __get() method is automatically called
Gender: Male
When directly obtaining the private attribute value, the __get() method is automatically called
Age: 20
If the above code is not added with the __get() and __set() methods, the program will make an error because private members cannot be operated outside the class. The above code helps us directly access the encapsulated private members by automatically calling the __get() and __set() methods.
__isset() method: Before looking at this method, let us look at the application of the "isset()" function. isset() is a function used to determine whether the variable is set. Pass a variable as a parameter. If the passed variable exists, it will be passed back true, otherwise it will be passed back false. So if you use the "isset()" function outside an object to determine whether the members in the object are set or not? There are two situations. If the members in the object are public, we can use this function to determine the member attributes. If it is a private member attribute, this function will not work. The reason is that the private one is encapsulated and is not visible outside. Then we cannot use the "isset()" function outside the object to determine whether the properties of the private member are set? Yes, you just need to add a "__isset()" method into the class. When using the "isset()" function outside the class 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 such operations. The "__isset()" method can also be made private. You can add the following code into the class:
private function__isset($nm)
{
echo "Automatically call when using isset() function to determine private member $nm outside the class <br>";
return isset($this->$nm);
}
__unset() method: Before looking at this method, let's take a look at the function "unset()". The function "unset()" is to delete the specified variable and pass back true, and the parameter is the variable to be deleted. So if you delete the member properties inside an object outside an object, can you use the "unset()" function? There are two situations. If the member properties inside an object are public, you can use this function to delete the public properties of the object outside the object. If the member properties of the object are private, I have no permission to delete it using this function. But if you add the "__unset()" method to an object, you can delete the private member properties of the object outside the object. After adding the method "__unset()" into the object, when using the "unset()" function outside the object to delete the private member attributes inside the object, the "__unset()" function is automatically called to help
We delete the private member attributes inside the object, and this method can also be defined as private within the class. Just add the following code to the object:
private function__unset($nm)
{
echo "It is called automatically when using the unset() function outside the class to delete private members<br>";
unset($this->$nm);
}
Let's look at a complete example:
<?php
classPerson
{
//The following are the member attributes of people
private $name; //The name of the person
private $sex; //People's gender
private $age; //People's age
//_get() method is used to obtain private attributes
private function__get($property_name)
{
if(isset($this->$property_name))
{
return($this->$property_name);
}else{
return(NULL);
}
}
//_set() method 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 determines a private member, it will be called automatically <br>";
return isset($this->$nm);
}
//_unset() method
private function__unset($nm)
{
echo "It is called automatically when using the unset() function outside the class to delete private members<br>";
unset($this->$nm);
}
}
$p1=newPerson();
$p1->name="this is a person name";
//When using the isset() function to measure private members, the __isset() method is automatically called to help us complete, and the return result is true
echovar_dump(isset($p1->name))."<br>";
echo $p1->name."<br>";
//When using the unset() function to delete private members, the __unset() method is automatically called to help us complete and delete the name private attribute
unset($p1->name);
//It has been deleted, so there will be no output in this line
echo $p1->name;
?>
The output result is:
When the isset() function determines a private member, it is automatically called
bool(true)
this is a person name
Automatically called when using the unset() function outside the class to delete private members
The four methods __set(), __get(), __isset(), and __unset() are all added to the object and are automatically called when needed to complete the operation of private properties inside the object outside the object.
Previous page12Read the full text