Home > Backend Development > PHP Tutorial > PHP 魔术函数

PHP 魔术函数

WBOY
Release: 2016-06-23 14:30:50
Original
928 people have browsed it

PHP魔术函数

class Person{    private $name;    private $age;    private $sex;    public function __construct($name,$age,$sex){        $this->name=$name;        $this->age=$age;        $this->sex=$sex;    }    public function __get($property){        if(isset($this->$property)){            return $this->$property;        }        else{            return null;        }    }    public function __set($property,$value){        $this->$property=$value;    }    //当在类外部使用isset()函数测定私有成员$nm时,自动调用    public function __isset($property){        return isset($this->$property);    }    //当在类外部使用unset()函数来删除私有成员时自动调用的    public function __unset($property){        unset($this->$property);    }    //在对象外部直接调用echo 对象名 的时候自动调用__toString()    public function __toString(){        return "hello world"."<br/>";    }    //在对象外面调用clone时,自动调用__clone()    public function __clone(){        $this->name="I am clone object";    }    //在对象外部调用对象没有的方法和参数的时候,取代报错,自动调用__call函数    public function __call($function_name, $args)    {        print "你所调用的函数:$function_name(参数:";        print_r($args);        print ")不存在!<br>\n";    }    //在对对象进行串行化的时候serialize,自动调用__sleep()    public function __sleep(){            }    //在对象进行反序列化的时候unserialize,自动调用wakeup()    public function __wakeup(){            }}function __autoload($classname){        require_once($classname.".php");}
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template