php的魔法方法__call使用

<?php

class Car
{
    public function __call($method,$args)
    {
        print "method $method called\n";
        var_dump($args);
    }
	
    /* 新版本php提供
    public static function __callStatic($method,$args)
    {
        print "static method $method called\n";
        var_dump($args);
    }
    */
}

$car = new Car();
$car->run();
// method run called array(0) { }

$a = 100;
$car->run($a);
// method run called array(1) { [0]=> int(100) }

$a = array(
    "a",
    "b" => array(
        1,2,3
    ),
    "c"
);
$car->run($a);
// method run called array(1) { [0]=> array(3) 
// { [0]=> string(1) "a" ["b"]=> array(3) { 
// [0]=> int(1) [1]=> int(2) [2]=> int(3) }
// [1]=> string(1) "c" } } 

$car = new Car();
$car->run("hello","world");
// method run called array(2) { [0]=> string(5) "hello" [1]=> string(5) "world" } 

/*
$car::alarm();
*/
?>
可以看出调用不存在的方法时,不会抛错“call method undefined”,而是进入到我们定义的__call魔法函数,第一个参数$method为出错的函数名,$args为传递给这个函数的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值