php程序参数使用常量,PHP如何将参数传递给常量

您可以使用func_get_args()和array_shift()来隔离常量字符串名称.

[

Codepad live]

class L {

const profile_tag_1 = 'Bla bla Age from %s to %s';

const profile_tag_2 = 'Wow Wow Age from %s to %s';

public static function __callStatic() {

$args = func_get_args();

$string = array_shift($args);

return vsprintf(constant('self::' . $string), $args);

}

}

L::__callStatic('profile_tag_1',12,12);

但是,请注意当使用此函数与静态方法的泛型调用时,您需要更改__callStatic签名以允许$name和$arguments,如下所示:

class L {

const profile_tag_1 = 'Bla bla Age from %s to %s';

const profile_tag_2 = 'Wow Wow Age from %s to %s';

public static function __callStatic($name, $args) {

$string = array_shift($args);

return vsprintf(constant('self::' . $string), $args);

}

}

L::format('profile_tag_1',12,12);

一个更好的方法

虽然,有一种更好的方法来执行你需要的东西(在评论中阅读Yoshi),考虑到你正在使用静态的东西:

echo sprintf(L::profile_tag_1,12,14);

此时你甚至不需要一个班级.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值