16 个 PHP 开发者必知必会的魔术方法( 三 )

代码的运行结果如下:
It is called automatically when we use the unset() method outside the class.1It is called automatically when we use the unset() method outside the class.19. __sleep()serialize () 方法将检查类中是否有魔术方法__sleep () 。如果存在,将首先调用该方法,然后执行序列化操作 。
 
__sleep () 方法通常用于指定保存数据之前需要序列化的属性 。如果有一些非常大的对象不需要全部保存,那么您会发现此功能非常有用 。
 
有关详细信息,请参考以下代码:
<?phpclass Person{public $sex;public $name;public $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}/*** @return array*/public function __sleep() {echo "It is called when the serialize() method is called outside the class.<br>";$this->name = base64_encode($this->name);return array('name', 'age'); // It must return a value of which the elements are the name of the properties returned.}}$person = new Person('John'); // Initially assigned.echo serialize($person);echo '<br/>';代码运行结果如下:
It is called when the serialize() method is called outside the class.O:6:"Person":2:{s:4:"name";s:8:"5bCP5piO";s:3:"age";i:25;}10. __wakeup()与 sleep () 方法相比,wakeup () 方法通常用于反序列化操作,例如重建数据库连接或执行其他初始化操作 。
 
下面是相关实例:
<?phpclass Person{public $sex;public $name;public $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}/*** @return array*/public function __sleep() {echo "It is called when the serialize() method is called outside the class.<br>";$this->name = base64_encode($this->name);return array('name', 'age'); // It must return a value of which the elements are the name of the properties returned.}/*** __wakeup*/public function __wakeup() {echo "It is called when the unserialize() method is called outside the class.<br>";$this->name = 2;$this->sex = 'Male';// There is no need to return an array here.}}$person = new Person('John'); // Initially assigned.var_dump(serialize($person));var_dump(unserialize(serialize($person)));代码运行结果如下:
It is called when the serialize() method is called outside the class.string(58) "O:6:"Person":2:{s:4:"name";s:8:"5bCP5piO";s:3:"age";i:25;}"It is called when the unserialize() method is called outside the class.object(Person)#2 (3) { ["sex"]=> string(3) "Male" ["name"]=> int(2) ["age"]=> int(25) }11. __toString()使用 echo 方法直接打印对象时,将调用__toString () 方法 。
注意:此方法必须返回一个字符串,否则将在 E_RECOVERABLE_ERROR 级别上引发致命错误 。而且您也不能在__toString () 方法中抛出异常 。
 
下面是相关的实例:
<?phpclass Person{public $sex;public $name;public $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}public function __toString(){return'go go go';}}$person = new Person('John'); // Initially assigned.echo $person;运行代码结果如下:
go go go那么,如果在类中未定义__toString () 方法怎么办?让我们尝试一下 。
 
<?phpclass Person{public $sex;public $name;public $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}}$person = new Person('John'); // Initially assigned.echo $person;运行代码结果如下:
Catchable fatal error: Object of class Person could not be converted to string in D:phpStudyWWWtestindex.php on line 18显然,它在页面上报告了一个致命错误,PHP 语法不支持这样的写法 。
 
12. __invoke()当您尝试以调用函数的方式调用对象时,__ invoke () 方法将被自动调用 。
注意:此功能仅在 PHP 5.3.0 及更高版本中有效 。
下面是相关实例:
<?phpclass Person{public $sex;public $name;public $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}public function __invoke() {echo 'This is an object';}}$person = new Person('John'); // Initially assigned.$person();运行代码结果如下:
This is an object


推荐阅读