__call() 使用技巧

在PHP中,__call() 是一个魔术方法,当调用一个对象中不可访问的方法(不存在或权限不足)时,这个方法会被自动调用。以下是一些使用 __call() 的技巧:

  1. 方法重定向: 使用 __call() 可以将调用重定向到另一个方法或对象。

php

复制

class MethodRedirector {
    private $helper;

    public function __construct() {
        $this->helper = new Helper();
    }

    public function __call($name,$arguments) {
        if (method_exists($this->helper,$name)) {
            return call_user_func_array([$this->helper,$name], $arguments);
        }
        throw new BadMethodCallException("Method {$name} does not exist");
    }
}

class Helper {
    public function assist() {
        echo "Helper method called";
    }
}

$redirector = new MethodRedirector();$redirector->assist(); // 输出: Helper method called
  1. 方法链式调用: 通过 __call() 实现链式调用,即使类中并没有定义那些方法。

php

复制

class Chainable {
    private $data = [];

    public function __call($name,$arguments) {
        if (strpos($name, 'set') === 0) {
            $property = lcfirst(substr($name, 3));
            $this->data[$property] = $arguments[0];
            return $this;
        } elseif (strpos($name, 'get') === 0) {
            $property = lcfirst(substr($name, 3));
            return $this->data[$property] ?? null;
        }
        throw new BadMethodCallException("Method {$name} does not exist");
    }
}

$chain = (new Chainable())->setName('John')->setAge(30);
echo $chain->getName(); // 输出: John
  1. 方法代理: 通过 __call() 代理调用其他对象的方法,可以用于实现代理模式。

php

复制

class Proxy {
    private $realSubject;

    public function __construct(RealSubject $realSubject) {
        $this->realSubject =$realSubject;
    }

    public function __call($name,$arguments) {
        return call_user_func_array([$this->realSubject,$name], $arguments);
    }
}

class RealSubject {
    public function doWork() {
        echo "Real work done";
    }
}

$proxy = new Proxy(new RealSubject());$proxy->doWork(); // 输出: Real work done
  1. 方法映射: 将方法名映射到其他方法,可以在不改变接口的情况下调整实现。

php

复制

class Mapper {
    public function __call($name,$arguments) {
        $methodMap = [
            'oldMethod' => 'newMethod',
        ];
        if (isset($methodMap[$name])) {
            return call_user_func_array([$this,$methodMap[$name]],$arguments);
        }
        throw new BadMethodCallException("Method {$name} does not exist");
    }

    private function newMethod() {
        echo "New method called";
    }
}

$mapper = new Mapper();$mapper->oldMethod(); // 输出: New method called
  1. 日志记录和调试: 使用 __call() 记录方法调用,对于调试和监控很有帮助。

php

复制

class Logger {
    public function __call($name,$arguments) {
        error_log("Attempt to call non-existent method: {$name}");
        throw new BadMethodCallException("Method {$name} does not exist");
    }
}

在使用 __call() 时,应当注意以下几点:

  • 避免过度使用,因为它可能会导致代码的意图变得不清晰。
  • 确保 __call() 方法的性能不会成为瓶颈。
  • 当 __call() 方法抛出异常时,应提供清晰的错误信息,以便调用者能够了解问题所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aheyor黄建珲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值