魔术法中的PHP vprintf
我的PHP包含作为常量的可翻译值。通过使用常量的名称调用类,应该返回格式化的值魔术法中的PHP vprintf,php,magic-methods,Php,Magic Methods,我的PHP包含作为常量的可翻译值。通过使用常量的名称调用类,应该返回格式化的值 class Test { const translation_value = 'Foo %s, bar %s'; public static function __callStatic($string, $args) { return vsprintf(constant("self::" . $string), $args); } } 如果我通过Test::{“transla
class Test {
const translation_value = 'Foo %s, bar %s';
public static function __callStatic($string, $args) {
return vsprintf(constant("self::" . $string), $args);
}
}
如果我通过Test::{“translation_value”}([“Test”,“test2”])调用该类代码>PHP显示两个错误:
注意:第4行的数组到字符串转换[…]
PHP警告:vsprintf():第4行上的参数[…]太少
我做错了什么
如果我通过手动调用来测试vprintf函数(vsprintf(“Foo%s,bar%s”,“val1”,“val2”);
),PHP将创建预期的输出:Foo val1,bar val2调用静态($string,$args)
的第二个参数是参数列表。您有1个参数,因此需要使用数组的第一个元素:
return vsprintf(constant("self::" . $string), $args[0]);
或者使用平面参数调用它:
Test::translation_value("test", "test2");
\u callStatic($string,$args)
的第二个参数是参数列表。您有1个参数,因此需要使用数组的第一个元素:
return vsprintf(constant("self::" . $string), $args[0]);
或者使用平面参数调用它:
Test::translation_value("test", "test2");