7.7.8 魔术方法__call()

7.7.8 魔术方法__call()
通过__call( )方法处理错误调用
当试图调用一个对象中不存在的方法时,就会产生错误。PHP提供了“__call()”这个方法来处理这种情况。即当调用一个不可访问方法(如未定义,或者不可见)时,__call()会被调用。
格式:
mixed __call( string $name , array $arguments )
说明:
第一个参数$name表示方法名,
第二参数$arguments表示调用时的参数列表(数组类型)

1.php

name = $name;
            $this->age = $age;
            $this->sex = $sex;
        }

        function say() {
            echo "我的名子是:{$this->name},我的年龄是:{$this->age},我的性别是:{$this->sex}。
"; } function __toString() { return "aaaaaaaaaaaaaaaaaaa
"; } function __destruct() { echo "{$this->name} ###########
"; } function __clone() { $this->name="克隆的"; $this->age=0; } function __call($method, $args) { echo "对不起!你调用的方法 {$method}(), 参数为"; print_r($args); echo "不存在!
"; } /* function aaa($a) { echo $a; } function bbb($b) { echo $b; } function ccc($c) { echo $c; } function ddd($d) { echo $d; } */ } $p = new Person("张三", 10, "男"); $p -> aaa("aaaaaaaaaaa"); $p -> bbb("bbbbbbbbbbbb");

test.php

name = $name;
            $this->age = $age;
            $this->sex = $sex;
        }

        function say() {
            echo "我的名子是:{$this->name},我的年龄是:{$this->age},我的性别是:{$this->sex}。
"; } function __toString() { return "aaaaaaaaaaaaaaaaaaa
"; } function __destruct() { echo "{$this->name} ###########
"; } function __clone() { $this->name="克隆的"; $this->age=0; } function __call($method, $args) { if(in_array($method, $this->marr)) { echo $args[0]."
"; }else{ echo "你调用的方法{$method}()不存在!
"; } } /* function aaa($a) { echo $a; } function bbb($b) { echo $b; } function ccc($c) { echo $c; } function ddd($d) { echo $d; } */ } $p = new Person("张三", 10, "男"); $p -> aaa("aaaaaaaaaaa"); $p -> bbb("bbbbbbbbbbbb"); $p -> ccc("cccccccccc"); $p -> ddd("dddddddd"); $p -> www("wwwwwwwww");

你可能感兴趣的:(7.7.8 魔术方法__call())