__set()
参数:$name,$value
执行过程:当在外部调用的类变量为不存在或不可见时,系统会首先调用__set()函数。
作用:可以用来设置错误信息,扩展类。
__get()
参数:$name
执行过程:当在外部获取非public类变量值时,系统会首先调用__get()函数。
__unset()
参数:$name
执行过程:当外部unset()函数的参数不存在时会调用__unset()函数,在内部可以对参数进行操作
例如:(引用自官方文档)
<?php
class PropertyTest {
/** 被重载的数据保存在此 */
private $data = array();
/** 重载不能被用在已经定义的属性 */
public $declared = 1;
/** 只有从类外部访问这个属性时,重载才会发生 */
private $hidden = 2;
public function __set($name, $value)
{
echo "Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
public function __get($name)
{
echo "Getting '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/** PHP 5.1.0之后版本 */
public function __isset($name)
{
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
/** PHP 5.1.0之后版本 */
public function __unset($name)
{
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
/** 非魔术方法 */
public function getHidden()
{
return $this->hidden;
}
}
echo "<pre>\n";
$obj = new PropertyTest;
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";
echo $obj->declared . "\n\n";
echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so __get() is used...\n";
echo $obj->hidden . "\n";
?>
__isset
参数:$name
执行过程:当外部用isset()函数来判断类内部的私有变量是否被调用时,就会触发系统调用魔术方法调用__isset()方法,可以在内部进行判断,将结果返回给外部。
例子:
<?php
class Animal{
public $name;
protected $age;
private $heigh;
function __construct($name,$age,$heigh){
$this->name=$name;
$this->age=$age;
$this->heigh=$heigh;
}
function __isset($name){
if($name='heigh'){
return isset($this->heigh);
}
}
}
$cat=new Animal("小花猫",5,20);
var_dump (isset($cat->heigh));
?>
__call
参数:$methodName(方法名),$arr (参数数组)
使用:当调用一个非公有的方法时,系统会自动调用__call()魔术方法,所以可以在__call()内部进行调用,像这样:
<?php
class Animal{
public $name;
protected $age;
private $heigh;
function __construct($name,$age,$heigh){
$this->name=$name;
$this->age=$age;
$this->heigh=$heigh;
}
public function __call($method,$arr){
$this->myCat();
}
protected function myCat(){
echo "我是一个非公共数组,请不要随意调用我";
}
}
$cat=new Animal("小花猫",5,20);
$cat->myCat();
?>
__callStatic
使用方法和上面的差不多,只不过方法改为静态方法
例子:
<?php
class Animal{
public $name;
protected $age;
private $heigh;
function __construct($name,$age,$heigh){
$this->name=$name;
$this->age=$age;
$this->heigh=$heigh;
}
public function __callStatic($method,$arr){
$this->myCat();
}
protected static function myCat(){
echo "我是一个非公共数组,请不要随意调用我";
}
}
$cat=new Animal("小花猫",5,20);
$cat->myCat();
?>
__autoload
参数:$clasName(类名字)
使用:实例化一个不存在的类时,会自动调用__autoload()魔术方法,可以在内部用include对方法进行引入
例子:
<?php
class Animal{
public $name;
public $age;
}
function __autoload($className){
//拼接文件名
$file="$className".".php";
//引入文件
include ($file);
}
$dog=new Dog();
$dog->outPrint();
?>
__toString()
无参数,有返回值,返回string类型
使用:当对对象进行打印输出时,会调用__toString魔术方法,接收返回的字符串类型。
例子:
<?php
class Animal{
public $name;
public $age;
function __toString(){
return "方法";
}
}
$dog=new Animal();
echo "$dog";
?>
__invoke()
调用对象时自动调用该方法,无参数,无返回值
<?php
class Animal {
public function __invoke()
{
echo '我是一只猫'.PHP_EOL;
}
}
$cat = new Animal();
$cat();
?>