PHP的戏法方法

WBOY
Release: 2016-06-13 12:22:30
Original
785 people have browsed it

PHP的魔术方法

1、__get/__set:对对象属性的接管
2、__call/__callStatic:控制php对象方法的使用
3、__toString:将php对象转换成字符串
4、__invoke:将一个php对象当成函数来执行时来回调这个方法

<code>class Object{ protected $array = array();    function __set($key,$value)    {        var_dump(__METHOD__);        $this->array[$key] = $value;    }    function __get($key)    {        var_dump(__METHOD__);        return $this->array[$key];    }    function __call($func,$param)    {        var_dump($func,$param);        return "magic mathod __call";    }    static function __callStatic($func,$param)    {        var_dump($func,$param);        return "magic mathod __callStatic";    }    function __toString()    {        return "__toString";    }    function __invoke($param)    {        var_dump($param);        return "invoke";    }}</code>
Copy after login

$Obj = new Object();

$Obj->title = “你好”;//当对一个对象不存在的属性赋值的时候,它就会自动调用__set方法

echo $Obj->title;//对去读取一个对象不存在的属性的时候,它就会自动调用__get方法

echo $Obj->test(“hello”,”123”); //当调用一个对象不存在的方法时,就会自动调用__call方法

echo $Obj::test1(“hello1”,”1234”); //当调用一个对象不存在的静态方法时,就会自动调用__callStatic方法

echo $Obj;//当直接输出一个对象时(因为对象不能直接输出),就会自动调用__toString方法把对象转换成字符串

echo $Obj(“hello”);//当将一个对象当成一个函数来使用的时候,就会自动调用__invoke方法

版权声明:本文为博主原创文章,未经博主允许不得转载。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!