PHP魔术方法详解

发布于:2024-03-18 ⋅ 阅读:(71) ⋅ 点赞:(0)

php魔术方法是一些特殊的方法,由特定的环境来进行触发。

这些魔术方法让开发者能够更好地控制对象的行为,特别是在处理不常见的操作或者需要自动化处理某些任务时非常有用。


1、_construct()构造函数:

<?php
highlight_file(__FILE__);
class User {
    public $username;
    public function __construct($username) {
        $this->username = $username;
        echo "触发了构造函数1次" ;
    }
}
$test = new User("benben");
$ser = serialize($test);
unserialize($ser);

?> 

_construct()构造函数,在创建实例化对象时自动调用。


2、_destruct()析构函数:

class User {
    public function __destruct()
    {
        echo "触发了析构函数1次"."<br />" ;
    }
}
$test = new User("benben");
$ser = serialize($test);
unserialize($ser);

_destruct()函数,在销毁对象时自动调用。


3、sleep()函数:

class User {
    const SITE = 'uusama';
    public $username;
    public $nickname;
    private $password;
    public function __construct($username, $nickname, $password)    {
        $this->username = $username;
        $this->nickname = $nickname;
        $this->password = $password;
    }
    public function __sleep() {
        return array('username', 'nickname');
    }
}
$user = new User('a', 'b', 'c');
echo serialize($user); 

_sleep()函数在对象被序列化时调用。


4、_wakeup()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    const SITE = 'uusama';
    public $username;
    public $nickname;
    private $password;
    private $order;
    public function __wakeup() {
        $this->password = $this->username;
    }
}
$user_ser = 'O:4:"User":2:{s:8:"username";s:1:"a";s:8:"nickname";s:1:"b";}';
var_dump(unserialize($user_ser));
?> 

_wakeup()函数在对象被反序列化时调用。


5、_toString()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    var $benben = "this is test!!";
         public function __toString()
         {
             return '格式不对,输出不了!';
          }
}
$test = new User() ;
print_r($test);
echo "<br />";
echo $test;
?> 

_toString()函数在将对象转化为字符串时调用。


6、invoke()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    var $benben = "this is test!!";
         public function __invoke()
         {
             echo  '它不是个函数!';
          }
}
$test = new User() ;
echo $test ->benben;
echo "<br />";
echo $test() ->benben;
?> 

_invoke()函数在将实例化对象test当作test()函数时调用。


7、_call()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    public function __call($arg1,$arg2)
    {
        echo "$arg1,$arg2[0]";
          }
}
$test = new User() ;
$test -> callxxx('a');
?> 

在对象调用一个不存在的·参数和方法时使用。


8、_callStatic()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    public function __callStatic($arg1,$arg2)
    {
        echo "$arg1,$arg2[0]";
          }
}
$test = new User() ;
$test::callxxx('a');
?> 

在以静态方式调用一个不可调用的方法时使用。


9、_set()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    public $var1;
    public function __set($arg1 ,$arg2)
    {
        echo  $arg1.','.$arg2;
    }
}
$test = new User() ;
$test ->var2=1;
?> 

给类中不存在的成员属性赋值时使用。


10、_get()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    public $var1;
    public function __get($arg1)
    {
        echo  $arg1;
    }
}
$test = new User() ;
$test ->var2;
?> 

调用类中不存在的属性时使用。


11、isset()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    private $var;
    public function __isset($arg1 )
    {
        echo  $arg1;
    }
}
$test = new User() ;
isset($test->var);
?> 

在使用isset()函数检查类中不可访问的属性是否存在时使用。


12、_unset()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    private $var;
    public function __unset($arg1 )
    {
        echo  $arg1;
    }
}
$test = new User() ;
unset($test->var);
?> 

在对不可访问的属性使用unset()时使用。


13、_clone()函数:

<?php
highlight_file(__FILE__);
error_reporting(0);
class User {
    private $var;
    public function __clone( )
    {
        echo  "__clone test";
          }
}
$test = new User() ;
$newclass = clone($test)
?> 

使用clone关键字拷贝克隆一个对象后,新对象会自动调用。


网站公告

今日签到

点亮在社区的每一天
去签到