php文件路径通配符,PHP函数中的通配符

我不确定术语“通配符”是否可以解释我的观点,但有时在一些现成的脚本中,我们可以调用一个非定义的函数,如find_by_age(23),其中age可以是映射到数据库表记录的任何其他内容.所以我可以调用find_by_name,find_by_email,find_by_id等等.那么我们怎么能以程序或面向对象的方式做这样的事情呢?

解决方法:

你正在寻找的术语是魔法.

基本上是这样的:

class Foo {

public function __call($method,$args) {

echo "You were looking for the method $method.\n";

}

}

$foo = new Foo();

$foo->bar(); // prints "You were looking for the method bar."

对于您正在寻找的内容,您只需过滤掉错误的函数调用并重定向好的函数:

class Model {

public function find_by_field_name($field,$value) { ... }

public function __call($method,$args) {

if (substr($method,0,8) === 'find_by_') {

$fn = array($this,'find_by_field_name');

$arguments = array_merge(array(substr($method,8)),$args);

return call_user_func_array($fn,$arguments);

} else {

throw new Exception("Method not found");

}

}

}

标签:php,function

来源: https://codeday.me/bug/20190606/1189941.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值