php所提供的重载(overloading)是指动态地创建类属性和方法。我们是通过魔术方法(magic methods)来实现的。
当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用。
所有的重载方法都必须被声明为 public。
注意:
1)这些魔术方法的参数都不能通过引用传递。
2)php中的重载与其它绝大多数面向对象语言不同。传统的重载是用于提供多个同名的类方法,但各方法的参数类型和个数不同。
属性重载
public void__set(string$name, mixed$value)
publicmixed__get(string$name)
publicbool__isset(string$name)
publicvoid__unset(string$name)
在给不可访问属性赋值时,__set() 会被调用。
读取不可访问属性的值时,__get() 会被调用。
当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。
当对不可访问属性调用 unset() 时,__unset() 会被调用。
参数 $name 是指要操作的变量名称。__set() 方法的 $value 参数指定了 $name 变量的值。
属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 php 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。
注意:
因为 php 处理赋值运算的方式,__set() 的返回值将被忽略。类似的, 在下面这样的链式赋值中,__get() 不会被调用:
$a = $obj->b = 8;
注意:
在除 isset() 外的其它语言结构中无法使用重载的属性,这意味着当对一个重载的属性使用 empty() 时,重载魔术方法将不会被调用。
为避开此限制,必须将重载属性赋值到本地变量再使用 empty()。
example #1 使用 __get(),__set(),__isset() 和 __unset() 进行属性重载
class propertytest{
/*被重载的数据保存在此*/
private $data = array();
/*重载不能被用在已经定义的属性*/
public $declared = 1;
/*只有从类外部访问这个属性时,重载才会发生*/
private $hidden = 2;
public function __set($name,$value)
{
echo "setting '$name' to '$value'<br>";
$this->data[$name] = $value;
}
public function __get($name)
{
echo "getting '$name'<br>";
if(array_key_exists($name,$this->data)){
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error('未知属性 via __get():'.$name.' in '.$trace[0]['file'].' on line '.$trace[0]['line'],
e_user_notice);
return null;
}
/*php5.1.0之后的版本*/
public function __isset($name){
echo "is '$name' set?<br>";
return isset($this->data[$name]);
}
/*php5.1.0之后的版本*/
public function __unset($name){
echo "unsetting '$name'<br>";
unset($this->data[$name]);
}
/*非魔术方法*/
public function gethidden(){
return $this->hidden;
}
}
echo '<pre>';
$obj = new propertytest;
$obj->a = 1;
echo $obj->a.'<br>';
var_dump(isset($obj->a));
unset($obj->a);
echo '<br>';
echo $obj->declared.'<br>';
echo "let's experiment with the private property named 'hidden':<br>";
echo "privates are visible inside the class,so __set() not used...<br>";
echo $obj->gethidden().'<br>';
echo "privates not visible outside of class,so __get() is used...<br>";
echo $obj->hidden;
输出结果:
setting 'a' to '1'
getting 'a'
1
is 'a' set?
bool(true)
unsetting 'a'
1
let's experiment with the private property named 'hidden':
privates are visible inside the class,so __set() not used...
2
privates not visible outside of class,so __get() is used...
getting 'hidden'
方法重载
public mixed __call ( string$name , array$arguments )
public static mixed __callstatic ( string$name , array$arguments )
在对象中调用一个不可访问方法时,__call() 会被调用。
用静态方式中调用一个不可访问方法时,__callstatic() 会被调用。
$name 参数是要调用的方法名称。$arguments 参数是一个枚举数组,包含着要传递给方法 $name 的参数。
example #2 使用 __call() 和 __callstatic() 对方法重载
class methodtest
{
public function __call($name,$arguments)
{
//注意:$name的值区分大小写
echo "calling object method '$name' ".implode(',',$argument).'<br>';
}
//php5.3.0之后的版本
public static function __callstatic($name,$arguments)
{
//注意:$name的值区分大小写
echo "calling static method '$name' ".implode(',',$argument).'<br>';
}
}
$obj = new methodtest;
$obj -> runtest('in object context');
methodtest::runtest('in static context');
输出结果:
calling object method 'runtest'
calling object method 'runtest'