___ en of and ___ 3en of

  • 2020-06-03 06:11:38
  • OfStack

php object-oriented _get(),_set() usage
In general, it is more logical to always define class attributes as private. However, the read and assign operations to the attributes are very frequent, so in PHP5, the two predefined functions "___ 6en ()" and "___ 7en ()" get and assign their attributes. Similar to javabean in java, the method is similar, except that set and get are not required for each field as in javabean. Just add two magic methods. That is, the operation of the set and value of a private member. In PHP5 provides us with the special method to get the attribute set values and value, "__set ()" and "__get ()" these two methods, these two methods is not the default, but we went to manually add to the class, as a constructor (__construct ()), 1 sample class added will exist, can press the following way to add these two methods, of course, can also according to the personal style to add:
 
//__set() Method to set a private property  
public function __set($name,$value){ 
$this->$name = $value; 
} 
//__get() Method is used to get private properties  
public function __get($name){ 
return $this->$name; 
} 

__get () method: this method is used to obtain private members of attribute values, with one parameter, the parameter passed in you want to get the names of the members of the attributes, return to obtain attribute values, this method need not call our manual, because we can also make this approach a private method, is the direct object automatically calls for private property. You can't get the value directly because the private property is already wrapped, but if you add this method to your class, use "echo$p1-" > A statement like "name" gets the value directly by calling the method with succget ($name), passes the property name to the argument $name, and through the internal execution of the method returns the value of the private property we passed in. If the member property is not encapsulated as private, the object itself will not automatically call the method.

___ () method: This method sets the value of the private member property with two arguments. The first argument is the property name with which you have to set the value, and the second argument is the property value with no return value. This method also does not need to be called manually. It can also be made private by automatically setting the value of the private property, which is also encapsulated
If you don't have the method with SUCCset (), don't allow it, say: $this- > name= 'zhangsan', this makes an error, but if you add the method with succset ($property_name, $value) to the class, and assign the property directly to the private property, it is automatically called. Pass the property, say name, to $property_name, pass the "zhangsan" to $value. If the member property is not encapsulated as private, the object itself will not automatically call the method. In order not to pass in illegal values, you can also make a 1 in this method. The code is as follows:
 
<?php 
class Person 
{ 
// Below are the person member properties, which are encapsulated private members  
private $name; // The name of the person  
private $sex; // A person's sex  
private $age; // A person's age  
//__get() Method is used to get private properties  
private function __get($property_name) 
{ 
echo " This is called automatically when you get the private property value directly __get() methods <br>"; 
if(isset($this->$property_name)) 
{ 
return($this->$property_name); 
} 
else 
{ 
return(NULL); 
} 
} 
//__set() Method to set a private property  
private function __set($property_name, $value) 
{ 
echo " This is called automatically when the private property value is set directly __set() Method assigns a value to a private property <br>"; 
$this->$property_name = $value; 
} 
} 
$p1=newPerson(); 
// An operation that assigns a value directly to a private property is called automatically __set() Method to assign a value  
$p1->name=" zhang 3"; 
$p1->sex=" male "; 
$p1->age=20; 
// Gets the value of the private property directly and is called automatically __get() Method, which returns the value of the member property  
echo " Name: ".$p1->name."<br>"; 
echo " Gender: ".$p1->sex."<br>"; 
echo " Age: ".$p1->age."<br>"; 
?> 

Program execution Results:
The method with SUCCset () is automatically called to assign a private property when the private property value is set directly
The method with SUCCset () is automatically called to assign a private property when the private property value is set directly
When you set the private property value directly, the method with SUCCset () is automatically called to assign the private property value
The method with succget () is automatically called when you get the private property value directly
Name: Zhang 3
The method with succget () is called automatically when you get the private property value directly
Gender: male
The method with succget () is called automatically when you get the private property value directly
Age: 20
If the above code does not include the methods of succget () and SUCCset (), the program will fail because it cannot operate on the private member outside of the class, and the code above helps us directly access the enclosed private member by automatically calling the methods of succget () and SUCCset ().