->Returns conversion_rate.
}The
Public function \u sleep(){
Return to [\
}The
Public function \u wakeup(){
$this->set\u conversion\u rate();
}The
\/**The
*@Param string $property
*@Param int$value
*\/The
Public function \u set ($property, $value){
If (0 male $property = (int) $value;
$this->set\u conversion\u rate();
}The
}The
Protected function set\conversion\u rate(){
If (0! = $this->total){
$this->conversion_rate = $this->conversions \/ $this->total;
}Another{
$this->conversion_rate = 0;
}The
}The
}This class uses the \u sleep() magic method to instruct PHP to serialize only the total and conversions attributes. Serialization of strings does not reduce much, but the database can save a lot of space if many such objects are stored or additional math is required. Although no magic method is used, there is a way to obtain the serialization like function provided by \u sleep() when converting objects to JSON. If the class implements the JSON serializable interface, there may be a method called JSON serializable() when passing it to the json\u encode() function to define the method for converting objects to JSON. The following is a new class, which extends the previous class and can be used to create JSON strings containing three attributes: Male set\u conversion\u rate();
Returns
\
\
‘conversion\u rate’= > $this->conversion\u rate
];
}The
}Use magic and learn more about magic. I hope this article shows how to use the magic methods I introduced or how to better use magic. I also hope this will help you understand other methods. For example, you have understood that \u get() and \u set() allow access to non-existent private or protected variables, so \u call() and \u callstatic() allow access to non-existent or private or protected variables. Mode of action. Review all the magic methods listed in the PHP manual and determine which other methods might be useful. However, magic can perform smart tasks, but that doesn’t mean magic must be used. Senior magicians will say that no matter how good your intentions are, too much magic will produce dangerous and unexpected results.