PHP之 拦截器的使用
PHP5支持三个内置的拦截器方法,它们类似于 __construct() 方法,当遇到合适的条件时就会被调用
拦截器方法
方法
描述
__get( $property )
访问未定义的属性时被调用
__set( $property, $value )
给未定义的属性赋值时被调用
__isset( $property)
对未定义的属性调用 isset() 时被调用
__unset( $property )
对未定义的属性调用unset()时被调用
__call( $method,$arg_array )
调用未定义的方法时被调用
__get( ) 和 __set( )方法用于处理类(或其父类)中未声明的属性。
当客户端代码试图访问未声明的属性时,__get( ) 会被调用,并带一个包含要访问的属性名称的字符串参数。
无论从__get()方法返回了什么,都会发送给客户端代码,就好像带有该值的目标属相存在一样。
理论不想说太多,下面是我从网上找的代码,可以参考学习
***************************************************************************************************************
如何使用?看代码
//拦截器的使用
class Computer{
private $name;
private $price;
private $cpu;
private $clocked;
//拦截器之赋值
public function __set($key,$value){
//那么:$key=name $value=”联想” 则有: $this->name=”联想”
return $this->$key=$value;
}
//拦截器之取值
public function __get($key){
if (isset($key)){
//那么: $key=name 则$this->name 所以自然就return了”联想”
return $this->$key;
}else {
return NULL;
}
}
}
//正是因为的拦截器存在,才能如此用
$computer=new Computer();
$computer->name=”联想”;
$computer->price=5600;
$computer->cpu=”八核”;
$computer->clocked=”1600hz”;
echo $computer->name;
echo $computer->price;
echo $computer->cpu;
echo $computer->clocked;
–>
****************************************************************************
_get()和__set()方法用于处理类(或其父类)中未声明的属性
——————-__get()———————-
class Person {
function __get($property){
$method = “get{$property}”;
if(method_exists($this,$method)){
return $this->$method();
}
}
public function getName(){
return “Bob”;
}
}
$p = new Person();
echo $p->name; //输出Bob
———————-__set()———————
class Person {
private $_name;
private $_age;
function __set($property,$value){
$method = “set{$property}”;
if(method_exists($this,$method)){
return $this->$method($value);
}
}
function setName($name){
$this->_name = $name;
if(!is_null($name)){
$this->_name = strtoupper($this->_name);
}
echo $this->_name;
}
}
$p = new Person();
$p->name = ‘bob’; //$p->_name为BOB;
__isset()和__get相对应,当在一个未定义的属性上调用isset()时,__isset被调用
———————__isset()————————-
function __isset($property){
$method = “get{$property}”;
return (method_exists($this,$method));
}
__unset()和__set相对应,当unset()在一个未定义的属性上被调用时,__unset()会被调用,并以该属性的名称作为参数,然后可以根据属性名进行必要的处理。
———————-__unset()————————
function __unset($property){
$method = “set{$property}”;
if(method_exists($this,$method)){
$this->$method(null);
}
}
__call()方法可能是最有用的拦截器方法,当程序中要调用类中未定义的方法时,__call()会被调用。__call()带有两参数,一个是方法名,一个是传递给要调用方法的所有参数(数组)。__call()方法返回的任何值都会返回给调用它的地方。
———————__call()————————–
class PersonWriter {
function writeName(Person $p){
echo $p->getName() . “\n”;
}
function writeAge(Person $p){
echo $p->getAge() . “\n”;
}
}
class Person {
private $writer;
function __construct(PersonWriter $writer){
$this->writer = $writer;
}
function __call($methodName,$args){
if(method_exists($this->writer,$methodName)){
return $this->writer->$methodName($this);
}
}
function getName(){
return ‘Bob’;
}
function getAge(){
return 44;
}
}
$person = new Person(new PersonWriter);
$person->writeName(); //输出 Bob
上面这个例子是通过__call()方法去实现委托。
标签:function, PHP