我的PHP以常量的形式包含可翻译的值。通过使用常量的名称调用类,应该返回格式化的值。
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"]);)来测试vprintf函数,PHP就会创建预期的输出:Foo val1,bar val2
发布于 2017-03-31 08:40:24
__callStatic($string, $args)的第二个参数是参数列表。您有一个参数,所以您需要使用数组的第一个元素:
return vsprintf(constant("self::" . $string), $args[0]);或者用平面参数调用它:
Test::translation_value("test", "test2");https://stackoverflow.com/questions/43136023
复制相似问题