首页 > 代码库 > php中的魔术方法
php中的魔术方法
php 中的魔术方法:
<?phpclass cat{ public function __get($property) { return $this->{$property}; } public function __call($parm1,$parm2) { print_r($parm1); print_r($parm2); } public function __set($var,$val) { echo $var."|".$val."\r\n"; $this->$var = $val; } public function __clone() { echo "clone func invoke"."\r\n"; } public function __isset($var) { echo "via __isset view isset".$var; } public function __unset($var) { echo "via __unset unset".$var; } public static function __callStatic($parm1,$parm2) { echo "__callStatic ".$parm1.var_dump($parm2); } public function __construct() { echo ‘__construct‘; } public function __destruct() { echo ‘__destruct‘; } public function __toString() { return ‘cat to string‘; } public function __invoke($xxx) { var_dump($xxx); } }$Cat = new cat();//echo $Cat->name;//$Cat->hello($array=array(‘a‘,‘b‘));$Cat->a = 3;$Cat2 = clone $Cat;$Cat2->a = 10;echo $Cat->a;echo $Cat2->a;var_dump(isset($Cat->b));unset($Cat->b);Cat::ddd(‘abc‘);echo $Cat2;$Cat(‘abccd‘);var_dump(is_callable($Cat));
此外还有:
<?php//student.class.phpclass Student{ private $full_name = ‘‘; private $score = 0; private $grades = array(); public function __construct($full_name, $score, $grades) { $this->full_name = $full_name; $this->grades = $grades; $this->score = $score; } public function show() { echo $this->full_name; } function __sleep() { echo ‘Going to sleep...‘; return array(‘full_name‘, ‘grades‘, ‘score‘); } function __wakeup() { echo ‘Waking up...‘; }}?><?php//a.php$student = new Student(‘bla bla‘, ‘a‘, array(‘a‘ => 90, ‘b‘ => 100));$student->show();echo "<br />\n";$s = serialize($student);echo $s ."<br />\n";$fp = fopen(‘./session.s‘, ‘w‘);fwrite($fp, $s);fclose($fp);?><?php//b.php$s = implode(‘‘, file("./session.s"));echo $s ."<br />\n";$a = unserialize($s);$a->show();?>
其中__invoke是5.3以后加入的。
php中的魔术方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。