Analysis of usage of various interceptors in php class, usage of php class interception
The examples in this article describe the usage of various interceptors in the PHP class. Share it with everyone for your reference. The specific usage analysis is as follows:
1. __get( $property ) is called when accessing undefined properties
Copy code The code is as follows:
class lanjie
{
Function __get($name)
{
echo $name." property not found! ";
}
}
$ob = new lanjie();
echo $ob->g;
When we call the undefined property g of object $ob, the interceptor __get() method is called and "g property not found!" is output;
2. __set( $property , $value ) assigns a value when calling an undefined property
Copy code The code is as follows:
class person
{
private $_age;
private $_name;
Function __set($name, $value)
{
$method = "set". ucfirst($name);
echo $method;
If(method_exists($this, $method) )
Return $this->$method( $value );
}
Function setName( $name )
{
$this->_name = $name;
If( !is_null($this->_name) )
$this->_name = strtoupper($this->_name);
}
Function setAge( $age )
{
return $this->_age = (int)$age;
}
}
$p = new person();
$p->name = 'bob';
print_r( array( $p ) );
Here we can clearly see that when assigning a value to an undefined 'name', "__set()" will be called
Others include __call(), __isset() , __unset();
The most useful and commonly used ones here are __call(), which is called when calling an existing method; __isset(), which is called when using the isset() function on an undefined attribute, and __unset, which is called when calling an undefined attribute. The defined number is called when using unset
I hope this article will be helpful to everyone’s PHP programming design.
Implementation of PHP interceptor
This Two is what you want..
What is the difference between interceptors and public attributes in php
http://www.bkjia.com/PHPjc/904926.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/904926.htmlTechArticleAnalysis of usage of various interceptors in php class, php class interception usage This article describes the various interceptor usage in php class with examples Interceptor usage. Share it with everyone for your reference. The specific usage analysis is as follows: 1....